<!DOCTYPE html>
<HTML lang = "en">
<HEAD>
  <meta charset="UTF-8"/>
  <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  <title>Optimizing Serial Code</title>
  

  <script type="text/x-mathjax-config">
    MathJax.Hub.Config({
      tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]},
      TeX: { equationNumbers: { autoNumber: "AMS" } }
    });
  </script>

  <script type="text/javascript" async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
  </script>

  
<style>
pre.hljl {
    border: 1px solid #ccc;
    margin: 5px;
    padding: 5px;
    overflow-x: auto;
    color: rgb(68,68,68); background-color: rgb(251,251,251); }
pre.hljl > span.hljl-t { }
pre.hljl > span.hljl-w { }
pre.hljl > span.hljl-e { }
pre.hljl > span.hljl-eB { }
pre.hljl > span.hljl-o { }
pre.hljl > span.hljl-k { color: rgb(148,91,176); font-weight: bold; }
pre.hljl > span.hljl-kc { color: rgb(59,151,46); font-style: italic; }
pre.hljl > span.hljl-kd { color: rgb(214,102,97); font-style: italic; }
pre.hljl > span.hljl-kn { color: rgb(148,91,176); font-weight: bold; }
pre.hljl > span.hljl-kp { color: rgb(148,91,176); font-weight: bold; }
pre.hljl > span.hljl-kr { color: rgb(148,91,176); font-weight: bold; }
pre.hljl > span.hljl-kt { color: rgb(148,91,176); font-weight: bold; }
pre.hljl > span.hljl-n { }
pre.hljl > span.hljl-na { }
pre.hljl > span.hljl-nb { }
pre.hljl > span.hljl-nbp { }
pre.hljl > span.hljl-nc { }
pre.hljl > span.hljl-ncB { }
pre.hljl > span.hljl-nd { color: rgb(214,102,97); }
pre.hljl > span.hljl-ne { }
pre.hljl > span.hljl-neB { }
pre.hljl > span.hljl-nf { color: rgb(66,102,213); }
pre.hljl > span.hljl-nfm { color: rgb(66,102,213); }
pre.hljl > span.hljl-np { }
pre.hljl > span.hljl-nl { }
pre.hljl > span.hljl-nn { }
pre.hljl > span.hljl-no { }
pre.hljl > span.hljl-nt { }
pre.hljl > span.hljl-nv { }
pre.hljl > span.hljl-nvc { }
pre.hljl > span.hljl-nvg { }
pre.hljl > span.hljl-nvi { }
pre.hljl > span.hljl-nvm { }
pre.hljl > span.hljl-l { }
pre.hljl > span.hljl-ld { color: rgb(148,91,176); font-style: italic; }
pre.hljl > span.hljl-s { color: rgb(201,61,57); }
pre.hljl > span.hljl-sa { color: rgb(201,61,57); }
pre.hljl > span.hljl-sb { color: rgb(201,61,57); }
pre.hljl > span.hljl-sc { color: rgb(201,61,57); }
pre.hljl > span.hljl-sd { color: rgb(201,61,57); }
pre.hljl > span.hljl-sdB { color: rgb(201,61,57); }
pre.hljl > span.hljl-sdC { color: rgb(201,61,57); }
pre.hljl > span.hljl-se { color: rgb(59,151,46); }
pre.hljl > span.hljl-sh { color: rgb(201,61,57); }
pre.hljl > span.hljl-si { }
pre.hljl > span.hljl-so { color: rgb(201,61,57); }
pre.hljl > span.hljl-sr { color: rgb(201,61,57); }
pre.hljl > span.hljl-ss { color: rgb(201,61,57); }
pre.hljl > span.hljl-ssB { color: rgb(201,61,57); }
pre.hljl > span.hljl-nB { color: rgb(59,151,46); }
pre.hljl > span.hljl-nbB { color: rgb(59,151,46); }
pre.hljl > span.hljl-nfB { color: rgb(59,151,46); }
pre.hljl > span.hljl-nh { color: rgb(59,151,46); }
pre.hljl > span.hljl-ni { color: rgb(59,151,46); }
pre.hljl > span.hljl-nil { color: rgb(59,151,46); }
pre.hljl > span.hljl-noB { color: rgb(59,151,46); }
pre.hljl > span.hljl-oB { color: rgb(102,102,102); font-weight: bold; }
pre.hljl > span.hljl-ow { color: rgb(102,102,102); font-weight: bold; }
pre.hljl > span.hljl-p { }
pre.hljl > span.hljl-c { color: rgb(153,153,119); font-style: italic; }
pre.hljl > span.hljl-ch { color: rgb(153,153,119); font-style: italic; }
pre.hljl > span.hljl-cm { color: rgb(153,153,119); font-style: italic; }
pre.hljl > span.hljl-cp { color: rgb(153,153,119); font-style: italic; }
pre.hljl > span.hljl-cpB { color: rgb(153,153,119); font-style: italic; }
pre.hljl > span.hljl-cs { color: rgb(153,153,119); font-style: italic; }
pre.hljl > span.hljl-csB { color: rgb(153,153,119); font-style: italic; }
pre.hljl > span.hljl-g { }
pre.hljl > span.hljl-gd { }
pre.hljl > span.hljl-ge { }
pre.hljl > span.hljl-geB { }
pre.hljl > span.hljl-gh { }
pre.hljl > span.hljl-gi { }
pre.hljl > span.hljl-go { }
pre.hljl > span.hljl-gp { }
pre.hljl > span.hljl-gs { }
pre.hljl > span.hljl-gsB { }
pre.hljl > span.hljl-gt { }
</style>



  <style type="text/css">
  @font-face {
  font-style: normal;
  font-weight: 300;
}
@font-face {
  font-style: normal;
  font-weight: 400;
}
@font-face {
  font-style: normal;
  font-weight: 600;
}
html {
  font-family: sans-serif; /* 1 */
  -ms-text-size-adjust: 100%; /* 2 */
  -webkit-text-size-adjust: 100%; /* 2 */
}
body {
  margin: 0;
}
article,
aside,
details,
figcaption,
figure,
footer,
header,
hgroup,
main,
menu,
nav,
section,
summary {
  display: block;
}
audio,
canvas,
progress,
video {
  display: inline-block; /* 1 */
  vertical-align: baseline; /* 2 */
}
audio:not([controls]) {
  display: none;
  height: 0;
}
[hidden],
template {
  display: none;
}
a:active,
a:hover {
  outline: 0;
}
abbr[title] {
  border-bottom: 1px dotted;
}
b,
strong {
  font-weight: bold;
}
dfn {
  font-style: italic;
}
h1 {
  font-size: 2em;
  margin: 0.67em 0;
}
mark {
  background: #ff0;
  color: #000;
}
small {
  font-size: 80%;
}
sub,
sup {
  font-size: 75%;
  line-height: 0;
  position: relative;
  vertical-align: baseline;
}
sup {
  top: -0.5em;
}
sub {
  bottom: -0.25em;
}
img {
  border: 0;
}
svg:not(:root) {
  overflow: hidden;
}
figure {
  margin: 1em 40px;
}
hr {
  -moz-box-sizing: content-box;
  box-sizing: content-box;
  height: 0;
}
pre {
  overflow: auto;
}
code,
kbd,
pre,
samp {
  font-family: monospace, monospace;
  font-size: 1em;
}
button,
input,
optgroup,
select,
textarea {
  color: inherit; /* 1 */
  font: inherit; /* 2 */
  margin: 0; /* 3 */
}
button {
  overflow: visible;
}
button,
select {
  text-transform: none;
}
button,
html input[type="button"], /* 1 */
input[type="reset"],
input[type="submit"] {
  -webkit-appearance: button; /* 2 */
  cursor: pointer; /* 3 */
}
button[disabled],
html input[disabled] {
  cursor: default;
}
button::-moz-focus-inner,
input::-moz-focus-inner {
  border: 0;
  padding: 0;
}
input {
  line-height: normal;
}
input[type="checkbox"],
input[type="radio"] {
  box-sizing: border-box; /* 1 */
  padding: 0; /* 2 */
}
input[type="number"]::-webkit-inner-spin-button,
input[type="number"]::-webkit-outer-spin-button {
  height: auto;
}
input[type="search"] {
  -webkit-appearance: textfield; /* 1 */
  -moz-box-sizing: content-box;
  -webkit-box-sizing: content-box; /* 2 */
  box-sizing: content-box;
}
input[type="search"]::-webkit-search-cancel-button,
input[type="search"]::-webkit-search-decoration {
  -webkit-appearance: none;
}
fieldset {
  border: 1px solid #c0c0c0;
  margin: 0 2px;
  padding: 0.35em 0.625em 0.75em;
}
legend {
  border: 0; /* 1 */
  padding: 0; /* 2 */
}
textarea {
  overflow: auto;
}
optgroup {
  font-weight: bold;
}
table {
  font-family: monospace, monospace;
  font-size : 0.8em;
  border-collapse: collapse;
  border-spacing: 0;
}
td,
th {
  padding: 0;
}
thead th {
    border-bottom: 1px solid black;
    background-color: white;
}
tr:nth-child(odd){
  background-color: rgb(248,248,248);
}


/*
* Skeleton V2.0.4
* Copyright 2014, Dave Gamache
* www.getskeleton.com
* Free to use under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
* 12/29/2014
*/
.container {
  position: relative;
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
  padding: 0 20px;
  box-sizing: border-box; }
.column,
.columns {
  width: 100%;
  float: left;
  box-sizing: border-box; }
@media (min-width: 400px) {
  .container {
    width: 85%;
    padding: 0; }
}
@media (min-width: 550px) {
  .container {
    width: 80%; }
  .column,
  .columns {
    margin-left: 4%; }
  .column:first-child,
  .columns:first-child {
    margin-left: 0; }

  .one.column,
  .one.columns                    { width: 4.66666666667%; }
  .two.columns                    { width: 13.3333333333%; }
  .three.columns                  { width: 22%;            }
  .four.columns                   { width: 30.6666666667%; }
  .five.columns                   { width: 39.3333333333%; }
  .six.columns                    { width: 48%;            }
  .seven.columns                  { width: 56.6666666667%; }
  .eight.columns                  { width: 65.3333333333%; }
  .nine.columns                   { width: 74.0%;          }
  .ten.columns                    { width: 82.6666666667%; }
  .eleven.columns                 { width: 91.3333333333%; }
  .twelve.columns                 { width: 100%; margin-left: 0; }

  .one-third.column               { width: 30.6666666667%; }
  .two-thirds.column              { width: 65.3333333333%; }

  .one-half.column                { width: 48%; }

  /* Offsets */
  .offset-by-one.column,
  .offset-by-one.columns          { margin-left: 8.66666666667%; }
  .offset-by-two.column,
  .offset-by-two.columns          { margin-left: 17.3333333333%; }
  .offset-by-three.column,
  .offset-by-three.columns        { margin-left: 26%;            }
  .offset-by-four.column,
  .offset-by-four.columns         { margin-left: 34.6666666667%; }
  .offset-by-five.column,
  .offset-by-five.columns         { margin-left: 43.3333333333%; }
  .offset-by-six.column,
  .offset-by-six.columns          { margin-left: 52%;            }
  .offset-by-seven.column,
  .offset-by-seven.columns        { margin-left: 60.6666666667%; }
  .offset-by-eight.column,
  .offset-by-eight.columns        { margin-left: 69.3333333333%; }
  .offset-by-nine.column,
  .offset-by-nine.columns         { margin-left: 78.0%;          }
  .offset-by-ten.column,
  .offset-by-ten.columns          { margin-left: 86.6666666667%; }
  .offset-by-eleven.column,
  .offset-by-eleven.columns       { margin-left: 95.3333333333%; }

  .offset-by-one-third.column,
  .offset-by-one-third.columns    { margin-left: 34.6666666667%; }
  .offset-by-two-thirds.column,
  .offset-by-two-thirds.columns   { margin-left: 69.3333333333%; }

  .offset-by-one-half.column,
  .offset-by-one-half.columns     { margin-left: 52%; }

}
html {
  font-size: 62.5%; }
body {
  font-size: 1.5em; /* currently ems cause chrome bug misinterpreting rems on body element */
  line-height: 1.6;
  font-weight: 400;
  font-family: "Raleway", "HelveticaNeue", "Helvetica Neue", Helvetica, Arial, sans-serif;
  color: #222; }
h1, h2, h3, h4, h5, h6 {
  margin-top: 0;
  margin-bottom: 2rem;
  font-weight: 300; }
h1 { font-size: 3.6rem; line-height: 1.2;  letter-spacing: -.1rem;}
h2 { font-size: 3.4rem; line-height: 1.25; letter-spacing: -.1rem; }
h3 { font-size: 3.2rem; line-height: 1.3;  letter-spacing: -.1rem; }
h4 { font-size: 2.8rem; line-height: 1.35; letter-spacing: -.08rem; }
h5 { font-size: 2.4rem; line-height: 1.5;  letter-spacing: -.05rem; }
h6 { font-size: 1.5rem; line-height: 1.6;  letter-spacing: 0; }

p {
  margin-top: 0; }
a {
  color: #1EAEDB; }
a:hover {
  color: #0FA0CE; }
.button,
button,
input[type="submit"],
input[type="reset"],
input[type="button"] {
  display: inline-block;
  height: 38px;
  padding: 0 30px;
  color: #555;
  text-align: center;
  font-size: 11px;
  font-weight: 600;
  line-height: 38px;
  letter-spacing: .1rem;
  text-transform: uppercase;
  text-decoration: none;
  white-space: nowrap;
  background-color: transparent;
  border-radius: 4px;
  border: 1px solid #bbb;
  cursor: pointer;
  box-sizing: border-box; }
.button:hover,
button:hover,
input[type="submit"]:hover,
input[type="reset"]:hover,
input[type="button"]:hover,
.button:focus,
button:focus,
input[type="submit"]:focus,
input[type="reset"]:focus,
input[type="button"]:focus {
  color: #333;
  border-color: #888;
  outline: 0; }
.button.button-primary,
button.button-primary,
input[type="submit"].button-primary,
input[type="reset"].button-primary,
input[type="button"].button-primary {
  color: #FFF;
  background-color: #33C3F0;
  border-color: #33C3F0; }
.button.button-primary:hover,
button.button-primary:hover,
input[type="submit"].button-primary:hover,
input[type="reset"].button-primary:hover,
input[type="button"].button-primary:hover,
.button.button-primary:focus,
button.button-primary:focus,
input[type="submit"].button-primary:focus,
input[type="reset"].button-primary:focus,
input[type="button"].button-primary:focus {
  color: #FFF;
  background-color: #1EAEDB;
  border-color: #1EAEDB; }
input[type="email"],
input[type="number"],
input[type="search"],
input[type="text"],
input[type="tel"],
input[type="url"],
input[type="password"],
textarea,
select {
  height: 38px;
  padding: 6px 10px; /* The 6px vertically centers text on FF, ignored by Webkit */
  background-color: #fff;
  border: 1px solid #D1D1D1;
  border-radius: 4px;
  box-shadow: none;
  box-sizing: border-box; }
/* Removes awkward default styles on some inputs for iOS */
input[type="email"],
input[type="number"],
input[type="search"],
input[type="text"],
input[type="tel"],
input[type="url"],
input[type="password"],
textarea {
  -webkit-appearance: none;
     -moz-appearance: none;
          appearance: none; }
textarea {
  min-height: 65px;
  padding-top: 6px;
  padding-bottom: 6px; }
input[type="email"]:focus,
input[type="number"]:focus,
input[type="search"]:focus,
input[type="text"]:focus,
input[type="tel"]:focus,
input[type="url"]:focus,
input[type="password"]:focus,
textarea:focus,
select:focus {
  border: 1px solid #33C3F0;
  outline: 0; }
label,
legend {
  display: block;
  margin-bottom: .5rem;
  font-weight: 600; }
fieldset {
  padding: 0;
  border-width: 0; }
input[type="checkbox"],
input[type="radio"] {
  display: inline; }
label > .label-body {
  display: inline-block;
  margin-left: .5rem;
  font-weight: normal; }
ul {
  list-style: circle; }
ol {
  list-style: decimal; }
ul ul,
ul ol,
ol ol,
ol ul {
  margin: 1.5rem 0 1.5rem 3rem;
  font-size: 90%; }
li > p {margin : 0;}
th,
td {
  padding: 12px 15px;
  text-align: left;
  border-bottom: 1px solid #E1E1E1; }
th:first-child,
td:first-child {
  padding-left: 0; }
th:last-child,
td:last-child {
  padding-right: 0; }
button,
.button {
  margin-bottom: 1rem; }
input,
textarea,
select,
fieldset {
  margin-bottom: 1.5rem; }
pre,
blockquote,
dl,
figure,
table,
p,
ul,
ol,
form {
  margin-bottom: 1.0rem; }
.u-full-width {
  width: 100%;
  box-sizing: border-box; }
.u-max-full-width {
  max-width: 100%;
  box-sizing: border-box; }
.u-pull-right {
  float: right; }
.u-pull-left {
  float: left; }
hr {
  margin-top: 3rem;
  margin-bottom: 3.5rem;
  border-width: 0;
  border-top: 1px solid #E1E1E1; }
.container:after,
.row:after,
.u-cf {
  content: "";
  display: table;
  clear: both; }

pre {
  display: block;
  padding: 9.5px;
  margin: 0 0 10px;
  font-size: 13px;
  line-height: 1.42857143;
  word-break: break-all;
  word-wrap: break-word;
  border: 1px solid #ccc;
  border-radius: 4px;
}

pre.hljl {
  margin: 0 0 10px;
  display: block;
  background: #f5f5f5;
  border-radius: 4px;
  padding : 5px;
}

pre.output {
  background: #ffffff;
}

pre.code {
  background: #ffffff;
}

pre.julia-error {
  color : red
}

code,
kbd,
pre,
samp {
  font-family: Menlo, Monaco, Consolas, "Courier New", monospace;
  font-size: 13px;
}


@media (min-width: 400px) {}
@media (min-width: 550px) {}
@media (min-width: 750px) {}
@media (min-width: 1000px) {}
@media (min-width: 1200px) {}

h1.title {margin-top : 20px}
img {max-width : 100%}
div.title {text-align: center;}

  </style>



</HEAD>
  <BODY>
    <div class ="container">
      <div class = "row">
        <div class = "col-md-12 twelve columns">

          <div class="title">
            <h1 class="title">Optimizing Serial Code</h1>
            <h5>Chris Rackauckas</h5>
            <h5>September 3rd, 2019</h5>
          </div>

          <p>At the center of any fast parallel code is a fast serial code. Parallelism is made to be a performance multiplier, so if you start from a bad position it won&#39;t ever get much better. Thus the first thing that we need to do is understand what makes code slow and how to avoid the pitfalls. This discussion of serial code optimization will also directly motivate why we will be using Julia throughout this course.</p>
<h2>Mental Model of a Memory</h2>
<p>To start optimizing code you need a good mental model of a computer.</p>
<h3>High Level View</h3>
<p>At the highest level you have a CPU&#39;s core memory which directly accesses a L1 cache. The L1 cache has the fastest access, so things which will be needed soon are kept there. However, it is filled from the L2 cache, which itself is filled from the L3 cache, which is filled from the main memory. This bring us to the first idea in optimizing code: using things that are already in a closer cache can help the code run faster because it doesn&#39;t have to be queried for and moved up this chain.</p>
<p><img src="https://hackernoon.com/hn-images/1*nT3RAGnOAWmKmvOBnizNtw.png" alt="" /></p>
<p>When something needs to be pulled directly from main memory this is known as a <em>cache miss</em>. To understand the cost of a cache miss vs standard calculations, take a look at <a href="http://ithare.com/infographics-operation-costs-in-cpu-clock-cycles/">this classic chart</a>.</p>
<p>&#40;Cache-aware and cache-oblivious algorithms are methods which change their indexing structure to optimize their use of the cache lines. We will return to this when talking about performance of linear algebra.&#41;</p>
<h3>Cache Lines and Row/Column-Major</h3>
<p>Many algorithms in numerical linear algebra are designed to minimize cache misses. Because of this chain, many modern CPUs try to guess what you will want next in your cache. When dealing with arrays, it will speculate ahead and grab what is known as a <em>cache line</em>: the next chunk in the array. Thus, your algorithms will be faster if you iterate along the values that it is grabbing.</p>
<p>The values that it grabs are the next values in the contiguous order of the stored array. There are two common conventions: row major and column major. Row major means that the linear array of memory is formed by stacking the rows one after another, while column major puts the column vectors one after another.</p>
<p><img src="https://eli.thegreenplace.net/images/2015/column-major-2D.png" alt="" /></p>
<p><em>Julia, MATLAB, and Fortran are column major</em>. Python&#39;s numpy is row-major.</p>


<pre class='hljl'>
<span class='hljl-n'>A</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>rand</span><span class='hljl-p'>(</span><span class='hljl-ni'>100</span><span class='hljl-p'>,</span><span class='hljl-ni'>100</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>B</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>rand</span><span class='hljl-p'>(</span><span class='hljl-ni'>100</span><span class='hljl-p'>,</span><span class='hljl-ni'>100</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>C</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>rand</span><span class='hljl-p'>(</span><span class='hljl-ni'>100</span><span class='hljl-p'>,</span><span class='hljl-ni'>100</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-k'>using</span><span class='hljl-t'> </span><span class='hljl-n'>BenchmarkTools</span><span class='hljl-t'>
</span><span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>inner_rows!</span><span class='hljl-p'>(</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>i</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>j</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-t'>
    </span><span class='hljl-n'>C</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>A</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>B</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'>
  </span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>inner_rows!</span><span class='hljl-p'>(</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
10.599 μs &#40;0 allocations: 0 bytes&#41;
</pre>



<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>inner_cols!</span><span class='hljl-p'>(</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>j</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>i</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-t'>
    </span><span class='hljl-n'>C</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>A</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>B</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'>
  </span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>inner_cols!</span><span class='hljl-p'>(</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
6.540 μs &#40;0 allocations: 0 bytes&#41;
</pre>


<h3>Lower Level View: The Stack and the Heap</h3>
<p>Locally, the stack is composed of a <em>stack</em> and a <em>heap</em>. The stack requires a static allocation: it is ordered. Because it&#39;s ordered, it is very clear where things are in the stack, and therefore accesses are very quick &#40;think instantanious&#41;. However, because this is static, it requires that the size of the variables is known at compile time &#40;to determine all of the variable locations&#41;. Since that is not possible with all variables, there exists the heap. The heap is essentially a stack of pointers to objects in memory. When heap variables are needed, their values are pulled up the cache chain and accessed.</p>
<p><img src="https://bayanbox.ir/view/581244719208138556/virtual-memory.jpg" alt="" /> <img src="https://camo.githubusercontent.com/ca96d70d09ce694363e44b93fd975bb3033898c1/687474703a2f2f7475746f7269616c732e6a656e6b6f762e636f6d2f696d616765732f6a6176612d636f6e63757272656e63792f6a6176612d6d656d6f72792d6d6f64656c2d352e706e67" alt="" /></p>
<h3>Heap Allocations and Speed</h3>
<p>Heap allocations are costly because they involve this pointer indirection, so stack allocation should be done when sensible &#40;it&#39;s not helpful for really large arrays, but for small values like scalars it&#39;s essential&#33;&#41;</p>


<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>inner_alloc!</span><span class='hljl-p'>(</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>j</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>i</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-t'>
    </span><span class='hljl-n'>val</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-p'>[</span><span class='hljl-n'>A</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>B</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]]</span><span class='hljl-t'>
    </span><span class='hljl-n'>C</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>val</span><span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-p'>]</span><span class='hljl-t'>
  </span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>inner_alloc!</span><span class='hljl-p'>(</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
208.801 μs &#40;10000 allocations: 937.50 KiB&#41;
</pre>



<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>inner_noalloc!</span><span class='hljl-p'>(</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>j</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>i</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-t'>
    </span><span class='hljl-n'>val</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>A</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>B</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'>
    </span><span class='hljl-n'>C</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>val</span><span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-p'>]</span><span class='hljl-t'>
  </span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>inner_noalloc!</span><span class='hljl-p'>(</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
6.520 μs &#40;0 allocations: 0 bytes&#41;
</pre>


<p>Why does the array here get heap-allocated? It isn&#39;t able to prove/guerentee at compile-time that the array&#39;s size will always be a given value, and thus it allocates it to the heap. <code>@btime</code> tells us this allocation occured and shows us the total heap memory that was taken. Meanwhile, the size of a Float64 number is known at compile-time &#40;64-bits&#41;, and so this is stored onto the stack and given a specific location that the compiler will be able to directly address.</p>
<p>Note that one can use the StaticArrays.jl library to get statically-sized arrays and thus arrays which are stack-allocated:</p>


<pre class='hljl'>
<span class='hljl-k'>using</span><span class='hljl-t'> </span><span class='hljl-n'>StaticArrays</span><span class='hljl-t'>
</span><span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>static_inner_alloc!</span><span class='hljl-p'>(</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>j</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>i</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-t'>
    </span><span class='hljl-n'>val</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nd'>@SVector</span><span class='hljl-t'> </span><span class='hljl-p'>[</span><span class='hljl-n'>A</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>B</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]]</span><span class='hljl-t'>
    </span><span class='hljl-n'>C</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>val</span><span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-p'>]</span><span class='hljl-t'>
  </span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>static_inner_alloc!</span><span class='hljl-p'>(</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
6.500 μs &#40;0 allocations: 0 bytes&#41;
</pre>


<h3>Mutation to Avoid Heap Allocations</h3>
<p>Many times you do need to write into an array, so how can you write into an array without performing a heap allocation? The answer is mutation. Mutation is changing the values of an already existing array. In that case, no free memory has to be found to put the array &#40;and no memory has to be freed by the garbage collector&#41;.</p>
<p>In Julia, functions which mutate the first value are conventionally noted by a <code>&#33;</code>. See the difference between these two equivalent functions:</p>


<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>inner_noalloc!</span><span class='hljl-p'>(</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>j</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>i</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-t'>
    </span><span class='hljl-n'>val</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>A</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>B</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'>
    </span><span class='hljl-n'>C</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>val</span><span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-p'>]</span><span class='hljl-t'>
  </span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>inner_noalloc!</span><span class='hljl-p'>(</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
6.540 μs &#40;0 allocations: 0 bytes&#41;
</pre>



<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>inner_alloc</span><span class='hljl-p'>(</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-n'>C</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>similar</span><span class='hljl-p'>(</span><span class='hljl-n'>A</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>j</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>i</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-t'>
    </span><span class='hljl-n'>val</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>A</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>B</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'>
    </span><span class='hljl-n'>C</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>val</span><span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-p'>]</span><span class='hljl-t'>
  </span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>inner_alloc</span><span class='hljl-p'>(</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
10.700 μs &#40;2 allocations: 78.20 KiB&#41;
</pre>


<p>To use this algorithm effectively, the <code>&#33;</code> algorithm assumes that the caller already has allocated the output array to put as the output argument. If that is not true, then one would need to manually allocate. The goal of that interface is to give the caller control over the allocations to allow them to manually reduce the total number of heap allocations and thus increase the speed.</p>
<h3>Julia&#39;s Broadcasting Mechanism</h3>
<p>Wouldn&#39;t it be nice to not have to write the loop there? In many high level languages this is simply called <em>vectorization</em>. In Julia, we will call it <em>array vectorization</em> to distinguish it from the <em>SIMD vectorization</em> which is common in lower level languages like C, Fortran, and Julia.</p>
<p>In Julia, if you use <code>.</code> on an operator it will transform it to the broadcasted form. Broadcast is <em>lazy</em>: it will build up an entire <code>.</code>&#39;d expression and then call <code>broadcast&#33;</code> on composed expression. This is customizable and <a href="https://docs.julialang.org/en/v1/manual/interfaces/#man-interfaces-broadcasting-1">documented in detail</a>. However, to a first approximation we can think of the broadcast mechanism as a mechanism for building <em>fused expressions</em>. For example, the Julia code:</p>


<pre class='hljl'>
<span class='hljl-n'>A</span><span class='hljl-t'> </span><span class='hljl-oB'>.+</span><span class='hljl-t'> </span><span class='hljl-n'>B</span><span class='hljl-t'> </span><span class='hljl-oB'>.+</span><span class='hljl-t'> </span><span class='hljl-n'>C</span><span class='hljl-p'>;</span>
</pre>



<p>under the hood lowers to something like:</p>


<pre class='hljl'>
<span class='hljl-nf'>map</span><span class='hljl-p'>((</span><span class='hljl-n'>a</span><span class='hljl-p'>,</span><span class='hljl-n'>b</span><span class='hljl-p'>,</span><span class='hljl-n'>c</span><span class='hljl-p'>)</span><span class='hljl-oB'>-&gt;</span><span class='hljl-n'>a</span><span class='hljl-oB'>+</span><span class='hljl-n'>b</span><span class='hljl-oB'>+</span><span class='hljl-n'>c</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>,</span><span class='hljl-n'>C</span><span class='hljl-p'>);</span>
</pre>



<p>where <code>map</code> is a function that just loops over the values element-wise.</p>
<p><strong>Take a quick second to think about why loop fusion may be an optimization.</strong></p>
<p>This about what would happen if you did not fuse the operations. We can write that out as:</p>


<pre class='hljl'>
<span class='hljl-n'>tmp</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>A</span><span class='hljl-t'> </span><span class='hljl-oB'>.+</span><span class='hljl-t'> </span><span class='hljl-n'>B</span><span class='hljl-t'>
</span><span class='hljl-n'>tmp</span><span class='hljl-t'> </span><span class='hljl-oB'>.+</span><span class='hljl-t'> </span><span class='hljl-n'>C</span><span class='hljl-p'>;</span>
</pre>



<p>Notice that if we did not fuse the expressions, we would need some place to put the result of <code>A .&#43; B</code>, and that would have to be an array, which means it would cause a heap allocation. Thus broadcast fusion eliminates the <em>temporary variable</em> &#40;colloquially called just a <em>temporary</em>&#41;.</p>


<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>unfused</span><span class='hljl-p'>(</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>,</span><span class='hljl-n'>C</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-n'>tmp</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>A</span><span class='hljl-t'> </span><span class='hljl-oB'>.+</span><span class='hljl-t'> </span><span class='hljl-n'>B</span><span class='hljl-t'>
  </span><span class='hljl-n'>tmp</span><span class='hljl-t'> </span><span class='hljl-oB'>.+</span><span class='hljl-t'> </span><span class='hljl-n'>C</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>unfused</span><span class='hljl-p'>(</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>,</span><span class='hljl-n'>C</span><span class='hljl-p'>);</span>
</pre>


<pre class="output">
12.999 μs &#40;4 allocations: 156.41 KiB&#41;
</pre>



<pre class='hljl'>
<span class='hljl-nf'>fused</span><span class='hljl-p'>(</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>,</span><span class='hljl-n'>C</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>A</span><span class='hljl-t'> </span><span class='hljl-oB'>.+</span><span class='hljl-t'> </span><span class='hljl-n'>B</span><span class='hljl-t'> </span><span class='hljl-oB'>.+</span><span class='hljl-t'> </span><span class='hljl-n'>C</span><span class='hljl-t'>
</span><span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>fused</span><span class='hljl-p'>(</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>,</span><span class='hljl-n'>C</span><span class='hljl-p'>);</span>
</pre>


<pre class="output">
5.875 μs &#40;2 allocations: 78.20 KiB&#41;
</pre>


<p>Note that we can also fuse the output by using <code>.&#61;</code>. This is essentially the vectorized version of a <code>&#33;</code> function:</p>


<pre class='hljl'>
<span class='hljl-n'>D</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>similar</span><span class='hljl-p'>(</span><span class='hljl-n'>A</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nf'>fused!</span><span class='hljl-p'>(</span><span class='hljl-n'>D</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>,</span><span class='hljl-n'>C</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-p'>(</span><span class='hljl-n'>D</span><span class='hljl-t'> </span><span class='hljl-oB'>.=</span><span class='hljl-t'> </span><span class='hljl-n'>A</span><span class='hljl-t'> </span><span class='hljl-oB'>.+</span><span class='hljl-t'> </span><span class='hljl-n'>B</span><span class='hljl-t'> </span><span class='hljl-oB'>.+</span><span class='hljl-t'> </span><span class='hljl-n'>C</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>fused!</span><span class='hljl-p'>(</span><span class='hljl-n'>D</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>,</span><span class='hljl-n'>C</span><span class='hljl-p'>);</span>
</pre>


<pre class="output">
3.775 μs &#40;0 allocations: 0 bytes&#41;
</pre>


<h3>Note on Broadcasting Function Calls</h3>
<p>Julia allows for broadcasting the call <code>&#40;&#41;</code> operator as well. <code>.&#40;&#41;</code> will call the function element-wise on all arguments, so <code>sin.&#40;A&#41;</code> will be the elementwise sine function. This will fuse Julia like the other operators.</p>
<h3>Note on Vectorization and Speed</h3>
<p>In articles on MATLAB, Python, R, etc., this is where you will be told to vectorize your code. Notice from above that this isn&#39;t a performance difference between writing loops and using vectorized broadcasts. This is not abnormal&#33; The reason why you are told to vectorize code in these other languages is because they have a high per-operation overhead &#40;which will be discussed further down&#41;. This means that every call, like <code>&#43;</code>, is costly in these languages. To get around this issue and make the language usable, someone wrote and compiled the loop for the C/Fortran function that does the broadcasted form &#40;see numpy&#39;s Github repo&#41;. Thus <code>A .&#43; B</code>&#39;s MATLAB/Python/R equivalents are calling a single C function to generally avoid the cost of function calls and thus are faster.</p>
<p>But this is not an intrinsic property of vectorization. Vectorization isn&#39;t &quot;fast&quot; in these languages, it&#39;s just close to the correct speed. The reason vectorization is recommended is because looping is slow in these languages. Because looping isn&#39;t slow in Julia &#40;or C, C&#43;&#43;, Fortran, etc.&#41;, loops and vectorization generally have the same speed. So use the one that works best for your code without a care about performance.</p>
<p>&#40;As a small side effect, these high level languages tend to allocate a lot of temporary variables since the individual C kernels are written for specific numbers of inputs and thus don&#39;t naturally fuse. Julia&#39;s broadcast mechanism is just generating and JIT compiling Julia functions on the fly, and thus it can accomodate the combinatorial explosion in the amount of choices just by only compiling the combinations that are necessary for a specific code&#41;</p>
<h3>Heap Allocations from Slicing</h3>
<p>It&#39;s important to note that slices in Julia produce copies instead of views. Thus for example:</p>


<pre class='hljl'>
<span class='hljl-n'>A</span><span class='hljl-p'>[</span><span class='hljl-ni'>50</span><span class='hljl-p'>,</span><span class='hljl-ni'>50</span><span class='hljl-p'>]</span>
</pre>


<pre class="output">
0.1671905216615932
</pre>


<p>allocates a new output. This is for safety, since if it pointed to the same array then writing to it would change the original array. We can demonstrate this by asking for a <em>view</em> instead of a copy.</p>


<pre class='hljl'>
<span class='hljl-nd'>@show</span><span class='hljl-t'> </span><span class='hljl-n'>A</span><span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-p'>]</span>
</pre>


<pre class="output">
A&#91;1&#93; &#61; 0.3898650195131119
</pre>



<pre class='hljl'>
<span class='hljl-n'>E</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nd'>@view</span><span class='hljl-t'> </span><span class='hljl-n'>A</span><span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>5</span><span class='hljl-p'>,</span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>5</span><span class='hljl-p'>]</span><span class='hljl-t'>
</span><span class='hljl-n'>E</span><span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nfB'>2.0</span><span class='hljl-t'>
</span><span class='hljl-nd'>@show</span><span class='hljl-t'> </span><span class='hljl-n'>A</span><span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-p'>]</span>
</pre>


<pre class="output">
A&#91;1&#93; &#61; 2.0
2.0
</pre>


<p>However, this means that <code>@view A&#91;1:5,1:5&#93;</code> did not allocate an array &#40;it does allocate a pointer if the escape analysis is unable to prove that it can be elided. This means that in small loops there will be no allocation, while if the view is returned from a function for example it will allocate the pointer, ~80 bytes, but not the memory of the array. This means that it is O&#40;1&#41; in cost but with a relatively small constant&#41;.</p>
<h3>Asymptopic Cost of Heap Allocations</h3>
<p>Heap allocations have to locate and prepare a space in RAM that is proportional to the amount of memory that is calcuated, which means that the cost of a heap allocation for an array is O&#40;n&#41;, with a large constant. As RAM begins to fill up, this cost dramatically increases. If you run out of RAM, your computer may begin to use <em>swap</em>, which is essentially RAM simulated on your hard drive. Generally when you hit swap your performance is so dead that you may think that your computation froze, but if you check your resource use you will notice that it&#39;s actually just filled the RAM and starting to use the swap.</p>
<p>But think of it as O&#40;n&#41; with a large constant factor. This means that for operations which only touch the data once, heap allocations can dominate the computational cost:</p>


<pre class='hljl'>
<span class='hljl-k'>using</span><span class='hljl-t'> </span><span class='hljl-n'>LinearAlgebra</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>BenchmarkTools</span><span class='hljl-t'>
</span><span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>alloc_timer</span><span class='hljl-p'>(</span><span class='hljl-n'>n</span><span class='hljl-p'>)</span><span class='hljl-t'>
    </span><span class='hljl-n'>A</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>rand</span><span class='hljl-p'>(</span><span class='hljl-n'>n</span><span class='hljl-p'>,</span><span class='hljl-n'>n</span><span class='hljl-p'>)</span><span class='hljl-t'>
    </span><span class='hljl-n'>B</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>rand</span><span class='hljl-p'>(</span><span class='hljl-n'>n</span><span class='hljl-p'>,</span><span class='hljl-n'>n</span><span class='hljl-p'>)</span><span class='hljl-t'>
    </span><span class='hljl-n'>C</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>rand</span><span class='hljl-p'>(</span><span class='hljl-n'>n</span><span class='hljl-p'>,</span><span class='hljl-n'>n</span><span class='hljl-p'>)</span><span class='hljl-t'>
    </span><span class='hljl-n'>t1</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nd'>@belapsed</span><span class='hljl-t'> </span><span class='hljl-oB'>$</span><span class='hljl-n'>A</span><span class='hljl-t'> </span><span class='hljl-oB'>.*</span><span class='hljl-t'> </span><span class='hljl-oB'>$</span><span class='hljl-n'>B</span><span class='hljl-t'>
    </span><span class='hljl-n'>t2</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nd'>@belapsed</span><span class='hljl-t'> </span><span class='hljl-p'>(</span><span class='hljl-oB'>$</span><span class='hljl-n'>C</span><span class='hljl-t'> </span><span class='hljl-oB'>.=</span><span class='hljl-t'> </span><span class='hljl-oB'>$</span><span class='hljl-n'>A</span><span class='hljl-t'> </span><span class='hljl-oB'>.*</span><span class='hljl-t'> </span><span class='hljl-oB'>$</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span><span class='hljl-t'>
    </span><span class='hljl-n'>t1</span><span class='hljl-p'>,</span><span class='hljl-n'>t2</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-n'>ns</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-t'> </span><span class='hljl-oB'>.^</span><span class='hljl-t'> </span><span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-oB'>:</span><span class='hljl-ni'>11</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>res</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-p'>[</span><span class='hljl-nf'>alloc_timer</span><span class='hljl-p'>(</span><span class='hljl-n'>n</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>n</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-n'>ns</span><span class='hljl-p'>]</span><span class='hljl-t'>
</span><span class='hljl-n'>alloc</span><span class='hljl-t'>   </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-p'>[</span><span class='hljl-n'>x</span><span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-n'>res</span><span class='hljl-p'>]</span><span class='hljl-t'>
</span><span class='hljl-n'>noalloc</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-p'>[</span><span class='hljl-n'>x</span><span class='hljl-p'>[</span><span class='hljl-ni'>2</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-n'>res</span><span class='hljl-p'>]</span><span class='hljl-t'>

</span><span class='hljl-k'>using</span><span class='hljl-t'> </span><span class='hljl-n'>Plots</span><span class='hljl-t'>
</span><span class='hljl-nf'>plot</span><span class='hljl-p'>(</span><span class='hljl-n'>ns</span><span class='hljl-p'>,</span><span class='hljl-n'>alloc</span><span class='hljl-p'>,</span><span class='hljl-n'>label</span><span class='hljl-oB'>=</span><span class='hljl-s'>&quot;=&quot;</span><span class='hljl-p'>,</span><span class='hljl-n'>xscale</span><span class='hljl-oB'>=:</span><span class='hljl-n'>log10</span><span class='hljl-p'>,</span><span class='hljl-n'>yscale</span><span class='hljl-oB'>=:</span><span class='hljl-n'>log10</span><span class='hljl-p'>,</span><span class='hljl-n'>legend</span><span class='hljl-oB'>=:</span><span class='hljl-n'>bottomright</span><span class='hljl-p'>,</span><span class='hljl-t'>
     </span><span class='hljl-n'>title</span><span class='hljl-oB'>=</span><span class='hljl-s'>&quot;Micro-optimizations matter for BLAS1&quot;</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nf'>plot!</span><span class='hljl-p'>(</span><span class='hljl-n'>ns</span><span class='hljl-p'>,</span><span class='hljl-n'>noalloc</span><span class='hljl-p'>,</span><span class='hljl-n'>label</span><span class='hljl-oB'>=</span><span class='hljl-s'>&quot;.=&quot;</span><span class='hljl-p'>)</span>
</pre>


<img src=""  />

<p>However, when the computation takes O&#40;n^3&#41;, like in matrix multiplications, the high constant factor only comes into play when the matrices are sufficiently small:</p>


<pre class='hljl'>
<span class='hljl-k'>using</span><span class='hljl-t'> </span><span class='hljl-n'>LinearAlgebra</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>BenchmarkTools</span><span class='hljl-t'>
</span><span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>alloc_timer</span><span class='hljl-p'>(</span><span class='hljl-n'>n</span><span class='hljl-p'>)</span><span class='hljl-t'>
    </span><span class='hljl-n'>A</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>rand</span><span class='hljl-p'>(</span><span class='hljl-n'>n</span><span class='hljl-p'>,</span><span class='hljl-n'>n</span><span class='hljl-p'>)</span><span class='hljl-t'>
    </span><span class='hljl-n'>B</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>rand</span><span class='hljl-p'>(</span><span class='hljl-n'>n</span><span class='hljl-p'>,</span><span class='hljl-n'>n</span><span class='hljl-p'>)</span><span class='hljl-t'>
    </span><span class='hljl-n'>C</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>rand</span><span class='hljl-p'>(</span><span class='hljl-n'>n</span><span class='hljl-p'>,</span><span class='hljl-n'>n</span><span class='hljl-p'>)</span><span class='hljl-t'>
    </span><span class='hljl-n'>t1</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nd'>@belapsed</span><span class='hljl-t'> </span><span class='hljl-oB'>$</span><span class='hljl-n'>A</span><span class='hljl-oB'>*$</span><span class='hljl-n'>B</span><span class='hljl-t'>
    </span><span class='hljl-n'>t2</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nd'>@belapsed</span><span class='hljl-t'> </span><span class='hljl-nf'>mul!</span><span class='hljl-p'>(</span><span class='hljl-oB'>$</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-oB'>$</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-oB'>$</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span><span class='hljl-t'>
    </span><span class='hljl-n'>t1</span><span class='hljl-p'>,</span><span class='hljl-n'>t2</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-n'>ns</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-t'> </span><span class='hljl-oB'>.^</span><span class='hljl-t'> </span><span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-oB'>:</span><span class='hljl-ni'>7</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>res</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-p'>[</span><span class='hljl-nf'>alloc_timer</span><span class='hljl-p'>(</span><span class='hljl-n'>n</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>n</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-n'>ns</span><span class='hljl-p'>]</span><span class='hljl-t'>
</span><span class='hljl-n'>alloc</span><span class='hljl-t'>   </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-p'>[</span><span class='hljl-n'>x</span><span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-n'>res</span><span class='hljl-p'>]</span><span class='hljl-t'>
</span><span class='hljl-n'>noalloc</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-p'>[</span><span class='hljl-n'>x</span><span class='hljl-p'>[</span><span class='hljl-ni'>2</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-n'>res</span><span class='hljl-p'>]</span><span class='hljl-t'>

</span><span class='hljl-k'>using</span><span class='hljl-t'> </span><span class='hljl-n'>Plots</span><span class='hljl-t'>
</span><span class='hljl-nf'>plot</span><span class='hljl-p'>(</span><span class='hljl-n'>ns</span><span class='hljl-p'>,</span><span class='hljl-n'>alloc</span><span class='hljl-p'>,</span><span class='hljl-n'>label</span><span class='hljl-oB'>=</span><span class='hljl-s'>&quot;*&quot;</span><span class='hljl-p'>,</span><span class='hljl-n'>xscale</span><span class='hljl-oB'>=:</span><span class='hljl-n'>log10</span><span class='hljl-p'>,</span><span class='hljl-n'>yscale</span><span class='hljl-oB'>=:</span><span class='hljl-n'>log10</span><span class='hljl-p'>,</span><span class='hljl-n'>legend</span><span class='hljl-oB'>=:</span><span class='hljl-n'>bottomright</span><span class='hljl-p'>,</span><span class='hljl-t'>
     </span><span class='hljl-n'>title</span><span class='hljl-oB'>=</span><span class='hljl-s'>&quot;Micro-optimizations only matter for small matmuls&quot;</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nf'>plot!</span><span class='hljl-p'>(</span><span class='hljl-n'>ns</span><span class='hljl-p'>,</span><span class='hljl-n'>noalloc</span><span class='hljl-p'>,</span><span class='hljl-n'>label</span><span class='hljl-oB'>=</span><span class='hljl-s'>&quot;mul!&quot;</span><span class='hljl-p'>)</span>
</pre>


<img src=""  />

<p>Though using a mutating form is never bad and always is a little bit better.</p>
<h3>Optimizing Memory Use Summary</h3>
<ul>
<li><p>Avoid cache misses by reusing values</p>
</li>
<li><p>Iterate along columns</p>
</li>
<li><p>Avoid heap allocations in inner loops</p>
</li>
<li><p>Heap allocations occur when the size of things is not proven at compile-time</p>
</li>
<li><p>Use fused broadcasts &#40;with mutated outputs&#41; to avoid heap allocations</p>
</li>
<li><p>Array vectorization confers no special benefit in Julia because Julia loops are as fast as C or Fortran</p>
</li>
<li><p>Use views instead of slices when applicable</p>
</li>
<li><p>Avoiding heap allocations is most necessary for O&#40;n&#41; algorithms or algorithms with small arrays</p>
</li>
<li><p>Use StaticArrays.jl to avoid heap allocations of small arrays in inner loops</p>
</li>
</ul>
<h2>Julia&#39;s Type Inference and the Compiler</h2>
<p>Many people think Julia is fast because it is JIT compiled. That is simply not true &#40;we&#39;ve already shown examples where Julia code isn&#39;t fast, but it&#39;s always JIT compiled&#33;&#41;. Instead, the reason why Julia is fast is because the combination of two ideas:</p>
<ul>
<li><p>Type inference</p>
</li>
<li><p>Type specialization in functions</p>
</li>
</ul>
<p>These two features naturally give rise to Julia&#39;s core design feature: multiple dispatch. Let&#39;s break down these pieces.</p>
<h3>Type Inference</h3>
<p>At the core level of the computer, everything has a type. Some languages are more explicit about said types, while others try to hide the types from the user. A type tells the compiler how to to store and interpret the memory of a value. For example, if the compiled code knows that the value in the register is supposed to be interpreted as a 64-bit floating point number, then it understands that slab of memory like:</p>
<p><img src="https://i.stack.imgur.com/ZUbLc.png" alt="" /></p>
<p>Importantly, it will know what to do for function calls. If the code tells it to add two floating point numbers, it will send them as inputs to the Floating Point Unit &#40;FPU&#41; which will give the output.</p>
<p>If the types are not known, then... ? So one cannot actually compute until the types are known, since otherwise it&#39;s impossible to interpret the memory. In languages like C, the programmer has to declare the types of variables in the program:</p>
<pre><code>void add&#40;double *a, double *b, double *c, size_t n&#41;&#123;
  size_t i;
  for&#40;i &#61; 0; i &lt; n; &#43;&#43;i&#41; &#123;
    c&#91;i&#93; &#61; a&#91;i&#93; &#43; b&#91;i&#93;;
  &#125;
&#125;</code></pre>
<p>The types are known at compile time because the programmer set it in stone. In many interpreted languages Python, types are checked at runtime. For example,</p>
<pre><code>a &#61; 2
b &#61; 4
a &#43; b</code></pre>
<p>when the addition occurs, the Python interpreter will check the object holding the values and ask it for its types, and use those types to know how to compute the &#43; function. For this reason, the add function in Python is rather complex since it needs to decode and have a version for all primitive types&#33;</p>
<p>Not only is there runtime overhead checks in function calls due to to not being explicit about types, there is also a memory overhead since it is impossible to know how much memory a value with take since that&#39;s a property of its type. Thus the Python interpreter cannot statically guerentee exact unchanging values for the size that a value would take in the stack, meaning that the variables are not stack-allocated. This means that every number ends up heap-allocated, which hopefully begins to explain why this is not as fast as C.</p>
<p>The solution is Julia is somewhat of a hybrid. The Julia code looks like:</p>


<pre class='hljl'>
<span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-t'>
</span><span class='hljl-n'>b</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>4</span><span class='hljl-t'>
</span><span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>b</span>
</pre>


<pre class="output">
6
</pre>


<p>However, before JIT compilation, Julia runs a type inference algorithm which finds out that <code>A</code> is an <code>Int</code>, and <code>B</code> is an <code>Int</code>. You can then understand that if it can prove that <code>A&#43;B</code> is an <code>Int</code>, then it can propogate all of the types through.</p>
<h3>Type Specialization in Functions</h3>
<p>Julia is able to propogate type inference through functions because, even if a function is &quot;untyped&quot;, Julia will interpret this as a <em>generic function</em> over possible <em>methods</em>, where every method has a concrete type. This means that in Julia, the function:</p>


<pre class='hljl'>
<span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>,</span><span class='hljl-n'>y</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-oB'>+</span><span class='hljl-n'>y</span>
</pre>


<pre class="output">
f &#40;generic function with 1 method&#41;
</pre>


<p>is not what you may think of as a &quot;single function&quot;, since given inputs of different types it will actually be a different function. We can see this by examining the <em>LLVM IR</em> &#40;LLVM is Julia&#39;s compiler, the IR is the <em>Intermediate Representation</em>, i.e. a platform-independent representation of assembly that lives in LLVM that it knows how to convert into assembly per architecture&#41;:</p>


<pre class='hljl'>
<span class='hljl-k'>using</span><span class='hljl-t'> </span><span class='hljl-n'>InteractiveUtils</span><span class='hljl-t'>
</span><span class='hljl-nd'>@code_llvm</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-p'>,</span><span class='hljl-ni'>5</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
;  @ none:1 within &#96;f&#39;
; Function Attrs: uwtable
define i64 @julia_f_15393&#40;i64, i64&#41; #0 &#123;
top:
; ┌ @ int.jl:53 within &#96;&#43;&#39;
   &#37;2 &#61; add i64 &#37;1, &#37;0
; └
  ret i64 &#37;2
&#125;
</pre>



<pre class='hljl'>
<span class='hljl-nd'>@code_llvm</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>5.0</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
;  @ none:1 within &#96;f&#39;
; Function Attrs: uwtable
define double @julia_f_15395&#40;double, double&#41; #0 &#123;
top:
; ┌ @ float.jl:395 within &#96;&#43;&#39;
   &#37;2 &#61; fadd double &#37;0, &#37;1
; └
  ret double &#37;2
&#125;
</pre>


<p>Notice that when <code>f</code> is the function that takes in two <code>Int</code>s, <code>Int</code>s add to give an <code>Int</code> and thus <code>f</code> outputs an <code>Int</code>. When <code>f</code> is the function that takes two <code>Float64</code>s, <code>f</code> returns a <code>Float64</code>. Thus in the code:</p>


<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>,</span><span class='hljl-n'>y</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>4</span><span class='hljl-t'>
  </span><span class='hljl-n'>b</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-t'>
  </span><span class='hljl-n'>c</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-n'>d</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>b</span><span class='hljl-p'>,</span><span class='hljl-n'>c</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>d</span><span class='hljl-p'>,</span><span class='hljl-n'>y</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>

</span><span class='hljl-nd'>@code_llvm</span><span class='hljl-t'> </span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-p'>,</span><span class='hljl-ni'>5</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
;  @ none:2 within &#96;g&#39;
; Function Attrs: uwtable
define i64 @julia_g_15396&#40;i64, i64&#41; #0 &#123;
top:
;  @ none:5 within &#96;g&#39;
; ┌ @ none:1 within &#96;f&#39;
; │┌ @ int.jl:53 within &#96;&#43;&#39;
    &#37;2 &#61; add i64 &#37;0, 6
; └└
;  @ none:6 within &#96;g&#39;
; ┌ @ none:1 within &#96;f&#39;
; │┌ @ int.jl:53 within &#96;&#43;&#39;
    &#37;3 &#61; add i64 &#37;2, &#37;1
; └└
  ret i64 &#37;3
&#125;
</pre>


<p><code>g</code> on two <code>Int</code> inputs is a function that has <code>Int</code>s at every step along the way and spits out an <code>Int</code>. We can use the <code>@code_warntype</code> macro to better see the inference along the steps of the function:</p>


<pre class='hljl'>
<span class='hljl-nd'>@code_warntype</span><span class='hljl-t'> </span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-p'>,</span><span class='hljl-ni'>5</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
Body::Int64
1 ─ &#37;1 &#61; π &#40;4, Core.Compiler.Const&#40;4, false&#41;&#41;
│   &#37;2 &#61; &#40;Base.add_int&#41;&#40;x, &#37;1&#41;::Int64
│   &#37;3 &#61; π &#40;2, Core.Compiler.Const&#40;2, false&#41;&#41;
│   &#37;4 &#61; &#40;Base.add_int&#41;&#40;&#37;3, &#37;2&#41;::Int64
│   &#37;5 &#61; &#40;Base.add_int&#41;&#40;&#37;4, y&#41;::Int64
└──      return &#37;5
</pre>


<p>What happens on mixtures?</p>


<pre class='hljl'>
<span class='hljl-nd'>@code_llvm</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>,</span><span class='hljl-ni'>5</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
;  @ none:1 within &#96;f&#39;
; Function Attrs: uwtable
define double @julia_f_15487&#40;double, i64&#41; #0 &#123;
top:
; ┌ @ promotion.jl:313 within &#96;&#43;&#39;
; │┌ @ promotion.jl:284 within &#96;promote&#39;
; ││┌ @ promotion.jl:261 within &#96;_promote&#39;
; │││┌ @ number.jl:7 within &#96;convert&#39;
; ││││┌ @ float.jl:60 within &#96;Type&#39;
       &#37;2 &#61; sitofp i64 &#37;1 to double
; │└└└└
; │ @ promotion.jl:313 within &#96;&#43;&#39; @ float.jl:395
   &#37;3 &#61; fadd double &#37;2, &#37;0
; └
  ret double &#37;3
&#125;
</pre>


<p>When we add an <code>Int</code> to a <code>Float64</code>, we promote the <code>Int</code> to a <code>Float64</code> and then perform the <code>&#43;</code> between two <code>Float64</code>s. When we go to the full function, we see that it can still infer:</p>


<pre class='hljl'>
<span class='hljl-nd'>@code_warntype</span><span class='hljl-t'> </span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>,</span><span class='hljl-ni'>5</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
Body::Float64
1 ─ &#37;1 &#61; &#40;Base.add_float&#41;&#40;x, 4.0&#41;::Float64
│   &#37;2 &#61; &#40;Base.add_float&#41;&#40;2.0, &#37;1&#41;::Float64
│   &#37;3 &#61; &#40;Base.sitofp&#41;&#40;Float64, y&#41;::Float64
│   &#37;4 &#61; &#40;Base.add_float&#41;&#40;&#37;2, &#37;3&#41;::Float64
└──      return &#37;4
</pre>


<p>and it uses this to build a very efficient assembly code because it knows exactly what the types will be at every step:</p>


<pre class='hljl'>
<span class='hljl-nd'>@code_llvm</span><span class='hljl-t'> </span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>,</span><span class='hljl-ni'>5</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
;  @ none:2 within &#96;g&#39;
; Function Attrs: uwtable
define double @julia_g_15496&#40;double, i64&#41; #0 &#123;
top:
;  @ none:4 within &#96;g&#39;
; ┌ @ none:1 within &#96;f&#39;
; │┌ @ promotion.jl:313 within &#96;&#43;&#39; @ float.jl:395
    &#37;2 &#61; fadd double &#37;0, 4.000000e&#43;00
; └└
;  @ none:5 within &#96;g&#39;
; ┌ @ none:1 within &#96;f&#39;
; │┌ @ promotion.jl:313 within &#96;&#43;&#39; @ float.jl:395
    &#37;3 &#61; fadd double &#37;2, 2.000000e&#43;00
; └└
;  @ none:6 within &#96;g&#39;
; ┌ @ none:1 within &#96;f&#39;
; │┌ @ promotion.jl:313 within &#96;&#43;&#39;
; ││┌ @ promotion.jl:284 within &#96;promote&#39;
; │││┌ @ promotion.jl:261 within &#96;_promote&#39;
; ││││┌ @ number.jl:7 within &#96;convert&#39;
; │││││┌ @ float.jl:60 within &#96;Type&#39;
        &#37;4 &#61; sitofp i64 &#37;1 to double
; ││└└└└
; ││ @ promotion.jl:313 within &#96;&#43;&#39; @ float.jl:395
    &#37;5 &#61; fadd double &#37;3, &#37;4
; └└
  ret double &#37;5
&#125;
</pre>


<p>&#40;notice how it handles the constant <em>literals</em> 4 and 2: it converted them at compile time to reduce the algorithm to 3 floating point additions&#41;.</p>
<h3>Type Stability</h3>
<p>Why is the inference algorithm able to infer all of the types of <code>g</code>? It&#39;s because it knows the types coming out of <code>f</code> at compile time. Given an <code>Int</code> and a <code>Float64</code>, <code>f</code> will always output a <code>Float64</code>, and thus it can continue with inference knowing that <code>c</code>, <code>d</code>, and eventually the output is <code>Float64</code>. Thus in order for this to occur, we need that the type of the output on our function is directly inferred from the type of the input. This property is known as type-stability.</p>
<p>An example of breaking it is as follows:</p>


<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>h</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>,</span><span class='hljl-n'>y</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-n'>out</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>y</span><span class='hljl-t'>
  </span><span class='hljl-nf'>rand</span><span class='hljl-p'>()</span><span class='hljl-t'> </span><span class='hljl-oB'>&lt;</span><span class='hljl-t'> </span><span class='hljl-nfB'>0.5</span><span class='hljl-t'> </span><span class='hljl-oB'>?</span><span class='hljl-t'> </span><span class='hljl-n'>out</span><span class='hljl-t'> </span><span class='hljl-oB'>:</span><span class='hljl-t'> </span><span class='hljl-nf'>Float64</span><span class='hljl-p'>(</span><span class='hljl-n'>out</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span>
</pre>


<pre class="output">
h &#40;generic function with 1 method&#41;
</pre>


<p>Here, on an integer input the output&#39;s type is randomly either Int or Float64, and thus the output is unknown:</p>


<pre class='hljl'>
<span class='hljl-nd'>@code_warntype</span><span class='hljl-t'> </span><span class='hljl-nf'>h</span><span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-p'>,</span><span class='hljl-ni'>5</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
Body::UNION&#123;FLOAT64, INT64&#125;
1 ── &#37;1  &#61; &#40;Base.add_int&#41;&#40;x, y&#41;::Int64
│    &#37;2  &#61; Random.GLOBAL_RNG::Random.MersenneTwister
│    &#37;3  &#61; &#40;Base.getfield&#41;&#40;&#37;2, :idxF&#41;::Int64
│    &#37;4  &#61; Random.MT_CACHE_F::Int64
│    &#37;5  &#61; &#40;&#37;3 &#61;&#61;&#61; &#37;4&#41;::Bool
└───       goto #3 if not &#37;5
2 ── &#37;7  &#61; &#36;&#40;Expr&#40;:gc_preserve_begin, :&#40;&#37;2&#41;&#41;&#41;
│    &#37;8  &#61; &#40;Base.getfield&#41;&#40;&#37;2, :state&#41;::Random.DSFMT.DSFMT_state
│    &#37;9  &#61; &#40;Base.getfield&#41;&#40;&#37;2, :vals&#41;::Array&#123;Float64,1&#125;
│    &#37;10 &#61; &#36;&#40;Expr&#40;:foreigncall, :&#40;:jl_array_ptr&#41;, Ptr&#123;Float64&#125;, svec&#40;Any&#41;, 
:&#40;:ccall&#41;, 1, :&#40;&#37;9&#41;&#41;&#41;::Ptr&#123;Float64&#125;
│    &#37;11 &#61; &#40;Base.getfield&#41;&#40;&#37;2, :vals&#41;::Array&#123;Float64,1&#125;
│    &#37;12 &#61; &#40;Base.arraylen&#41;&#40;&#37;11&#41;::Int64
│          invoke Random.dsfmt_fill_array_close1_open2&#33;&#40;&#37;8::Random.DSFMT.DS
FMT_state, &#37;10::Ptr&#123;Float64&#125;, &#37;12::Int64&#41;
│          &#36;&#40;Expr&#40;:gc_preserve_end, :&#40;&#37;7&#41;&#41;&#41;
└───       &#40;Base.setfield&#33;&#41;&#40;&#37;2, :idxF, 0&#41;
3 ┄─       goto #4
4 ── &#37;17 &#61; &#40;Base.getfield&#41;&#40;&#37;2, :vals&#41;::Array&#123;Float64,1&#125;
│    &#37;18 &#61; &#40;Base.getfield&#41;&#40;&#37;2, :idxF&#41;::Int64
│    &#37;19 &#61; &#40;Base.add_int&#41;&#40;&#37;18, 1&#41;::Int64
│          &#40;Base.setfield&#33;&#41;&#40;&#37;2, :idxF, &#37;19&#41;
│    &#37;21 &#61; &#40;Base.arrayref&#41;&#40;false, &#37;17, &#37;19&#41;::Float64
└───       goto #5
5 ──       goto #6
6 ── &#37;24 &#61; &#40;Base.sub_float&#41;&#40;&#37;21, 1.0&#41;::Float64
└───       goto #7
7 ──       goto #8
8 ──       goto #9
9 ── &#37;28 &#61; &#40;Base.lt_float&#41;&#40;&#37;24, 0.5&#41;::Bool
└───       goto #11 if not &#37;28
10 ─       return &#37;1
11 ─ &#37;31 &#61; &#40;Base.sitofp&#41;&#40;Float64, &#37;1&#41;::Float64
└───       return &#37;31
</pre>


<p>This means that its output type is <code>Union&#123;Int,Float64&#125;</code> &#40;Julia uses union types to keep the types still somewhat constrained&#41;. Once there are multiple choices, those need to get propogated through the compiler, and all subsequent calculations are the result of either being an <code>Int</code> or a <code>Float64</code>.</p>
<p>&#40;Note that Julia has small union optimizations, so if this union is of size 4 or less then Julia will still be able to optimize it quite a bit.&#41;</p>
<h3>Multiple Dispatch</h3>
<p>The <code>&#43;</code> function on numbers was implemented in Julia, so how were these rules all written down? The answer is multiple dispatch. In Julia, you can tell a function how to act differently on different types by using type assertions on the input values. For example, let&#39;s make a function that computes <code>2x &#43; y</code> on <code>Int</code> and <code>x/y</code> on <code>Float64</code>:</p>


<pre class='hljl'>
<span class='hljl-nf'>ff</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-oB'>::</span><span class='hljl-n'>Int</span><span class='hljl-p'>,</span><span class='hljl-n'>y</span><span class='hljl-oB'>::</span><span class='hljl-n'>Int</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>y</span><span class='hljl-t'>
</span><span class='hljl-nf'>ff</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-oB'>::</span><span class='hljl-n'>Float64</span><span class='hljl-p'>,</span><span class='hljl-n'>y</span><span class='hljl-oB'>::</span><span class='hljl-n'>Float64</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-oB'>/</span><span class='hljl-n'>y</span><span class='hljl-t'>
</span><span class='hljl-nd'>@show</span><span class='hljl-t'> </span><span class='hljl-nf'>ff</span><span class='hljl-p'>(</span><span class='hljl-ni'>2</span><span class='hljl-p'>,</span><span class='hljl-ni'>5</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
ff&#40;2, 5&#41; &#61; 9
</pre>



<pre class='hljl'>
<span class='hljl-nd'>@show</span><span class='hljl-t'> </span><span class='hljl-nf'>ff</span><span class='hljl-p'>(</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>5.0</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
ff&#40;2.0, 5.0&#41; &#61; 0.4
0.4
</pre>


<p>The <code>&#43;</code> function in Julia is just defined as <code>&#43;&#40;a,b&#41;</code>, and we can actually point to that code in the Julia distribution:</p>


<pre class='hljl'>
<span class='hljl-nd'>@which</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-p'>(</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>,</span><span class='hljl-ni'>5</span><span class='hljl-p'>)</span>
</pre>



+(x::<b>Number</b>, y::<b>Number</b>) in Base at <a href="https://github.com/JuliaLang/julia/tree/80516ca20297a67b996caa08c38786332379b6a5/base/promotion.jl#L313" target="_blank">promotion.jl:313</a>

<p>To control at a higher level, Julia uses <em>abstract types</em>. For example, <code>Float64 &lt;: AbstractFloat</code>, meaning <code>Float64</code>s are a subtype of <code>AbstractFloat</code>. We also have that <code>Int &lt;: Integer</code>, while both <code>AbstractFloat &lt;: Number</code> and <code>Integer &lt;: Number</code>.</p>
<p><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Type-hierarchy-for-julia-numbers.png/800px-Type-hierarchy-for-julia-numbers.png" alt="" /></p>
<p>Julia allows the user to define dispatches at a higher level, and the version that is called is the most strict version that is correct. For example, right now with <code>ff</code> we will get a <code>MethodError</code> if we call it between a <code>Int</code> and a <code>Float64</code> because no such method exists:</p>


<pre class='hljl'>
<span class='hljl-nf'>ff</span><span class='hljl-p'>(</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>,</span><span class='hljl-ni'>5</span><span class='hljl-p'>)</span>
</pre>


<pre class="julia-error">
ERROR: MethodError: no method matching ff&#40;::Float64, ::Int64&#41;
Closest candidates are:
  ff&#40;::Float64, &#33;Matched::Float64&#41; at none:1
  ff&#40;&#33;Matched::Int64, ::Int64&#41; at none:1
</pre>


<p>However, we can add a <em>fallback method</em> to the function <code>ff</code> for two numbers:</p>


<pre class='hljl'>
<span class='hljl-nf'>ff</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-oB'>::</span><span class='hljl-n'>Number</span><span class='hljl-p'>,</span><span class='hljl-n'>y</span><span class='hljl-oB'>::</span><span class='hljl-n'>Number</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>y</span><span class='hljl-t'>
</span><span class='hljl-nf'>ff</span><span class='hljl-p'>(</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>,</span><span class='hljl-ni'>5</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
7.0
</pre>


<p>Notice that the fallback method still specailizes on the inputs:</p>


<pre class='hljl'>
<span class='hljl-nd'>@code_llvm</span><span class='hljl-t'> </span><span class='hljl-nf'>ff</span><span class='hljl-p'>(</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>,</span><span class='hljl-ni'>5</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
;  @ none:1 within &#96;ff&#39;
; Function Attrs: uwtable
define double @julia_ff_15565&#40;double, i64&#41; #0 &#123;
top:
; ┌ @ promotion.jl:313 within &#96;&#43;&#39;
; │┌ @ promotion.jl:284 within &#96;promote&#39;
; ││┌ @ promotion.jl:261 within &#96;_promote&#39;
; │││┌ @ number.jl:7 within &#96;convert&#39;
; ││││┌ @ float.jl:60 within &#96;Type&#39;
       &#37;2 &#61; sitofp i64 &#37;1 to double
; │└└└└
; │ @ promotion.jl:313 within &#96;&#43;&#39; @ float.jl:395
   &#37;3 &#61; fadd double &#37;2, &#37;0
; └
  ret double &#37;3
&#125;
</pre>


<p>It&#39;s essentially just a template for what functions to possibly try and create given the types that are seen. When it sees <code>Float64</code> and <code>Int</code>, it knows it should try and create the function that does <code>x&#43;y</code>, and once it knows it&#39;s <code>Float64</code> plus a <code>Int</code>, it knows it should create the function that converts the <code>Int</code> to a <code>Float64</code> and then does addition between two <code>Float64</code>s, and that is precisely the generated LLVM IR on this pair of input types.</p>
<p>And that&#39;s essentially Julia&#39;s secret sauce: since it&#39;s always specializing its types on each function, if those functions themselves can infer the output, then the entire function can be inferred and generate optimal code, which is then optimized by the compiler and out comes an efficient function. If types can&#39;t be inferred, Julia falls back to a slower &quot;Python&quot; mode &#40;though with optimizations in cases like small unions&#41;. Users then get control over this specialization process through multiple dispatch, which is then Julia&#39;s core feature since it allows adding new options without any runtime cost.</p>
<h3>Any Fallbacks</h3>
<p>Note that <code>f&#40;x,y&#41; &#61; x&#43;y</code> is equivalent to <code>f&#40;x::Any,y::Any&#41; &#61; x&#43;y</code>, where <code>Any</code> is the maximal supertype of every Julia type. Thus <code>f&#40;x,y&#41; &#61; x&#43;y</code> is essentially a fallback for all possible input values, telling it what to do in the case that no other dispatches exist. However, note that this dispatch itself is not slow, since it will be specailized on the input types.</p>
<h3>Ambiguities</h3>
<p>The version that is called is the most strict version that is correct. What happens if it&#39;s impossible to define &quot;the most strict version&quot;? For example,</p>


<pre class='hljl'>
<span class='hljl-nf'>ff</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-oB'>::</span><span class='hljl-n'>Float64</span><span class='hljl-p'>,</span><span class='hljl-n'>y</span><span class='hljl-oB'>::</span><span class='hljl-n'>Number</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>5</span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-n'>y</span><span class='hljl-t'>
</span><span class='hljl-nf'>ff</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-oB'>::</span><span class='hljl-n'>Number</span><span class='hljl-p'>,</span><span class='hljl-n'>y</span><span class='hljl-oB'>::</span><span class='hljl-n'>Int</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>-</span><span class='hljl-t'> </span><span class='hljl-n'>y</span>
</pre>


<pre class="output">
ff &#40;generic function with 5 methods&#41;
</pre>


<p>What should it call on <code>f&#40;2.0,5&#41;</code> now?  <code>ff&#40;x::Float64,y::Number&#41;</code> and <code>ff&#40;x::Number,y::Int&#41;</code> are both more strict than <code>ff&#40;x::Number,y::Number&#41;</code>, so one of them should be called, but neither are more strict than each other, and thus you will end up with an ambiguity error:</p>


<pre class='hljl'>
<span class='hljl-nf'>ff</span><span class='hljl-p'>(</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>,</span><span class='hljl-ni'>5</span><span class='hljl-p'>)</span>
</pre>


<pre class="julia-error">
ERROR: MethodError: Main.WeaveSandBox0.ff&#40;::Float64, ::Int64&#41; is ambiguous. Candidates:
  ff&#40;x::Number, y::Int64&#41; in Main.WeaveSandBox0 at none:1
  ff&#40;x::Float64, y::Number&#41; in Main.WeaveSandBox0 at none:1
Possible fix, define
  ff&#40;::Float64, ::Int64&#41;
</pre>


<h3>Untyped Containers</h3>
<p>One way to ruin inference is to use an untyped container. For example, the array constructors use type inference themselves to know what their container type will be. Therefore,</p>


<pre class='hljl'>
<span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-p'>[</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>3.0</span><span class='hljl-p'>]</span>
</pre>


<pre class="output">
3-element Array&#123;Float64,1&#125;:
 1.0
 2.0
 3.0
</pre>


<p>uses type inference on its inputs to know that it should be something that holds <code>Float64</code> values, and thus it is a 1-dimensional array of <code>Float64</code> values, or <code>Array&#123;Float64,1&#125;</code>. The accesses:</p>


<pre class='hljl'>
<span class='hljl-n'>a</span><span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-p'>]</span>
</pre>


<pre class="output">
1.0
</pre>


<p>are then inferred, since this is just the function <code>getindex&#40;a::Array&#123;T&#125;,i&#41; where T</code> which is a function that will produce something of type <code>T</code>, the element type of the array. However, if we tell Julia to make an array with element type <code>Any</code>:</p>


<pre class='hljl'>
<span class='hljl-n'>b</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-p'>[</span><span class='hljl-s'>&quot;1.0&quot;</span><span class='hljl-p'>,</span><span class='hljl-ni'>2</span><span class='hljl-p'>,</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>]</span>
</pre>


<pre class="output">
3-element Array&#123;Any,1&#125;:
  &quot;1.0&quot;
 2     
 2.0
</pre>


<p>&#40;here, Julia falls back to <code>Any</code> because it cannot promote the values to the same type&#41;, then the best inference can do on the output is to say it could have any type:</p>


<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>bad_container</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-n'>a</span><span class='hljl-p'>[</span><span class='hljl-ni'>2</span><span class='hljl-p'>]</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nd'>@code_warntype</span><span class='hljl-t'> </span><span class='hljl-nf'>bad_container</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
Body::Float64
1 ─ &#37;1 &#61; &#40;Base.arrayref&#41;&#40;true, a, 2&#41;::Float64
└──      return &#37;1
</pre>



<pre class='hljl'>
<span class='hljl-nd'>@code_warntype</span><span class='hljl-t'> </span><span class='hljl-nf'>bad_container</span><span class='hljl-p'>(</span><span class='hljl-n'>b</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
Body::ANY
1 ─ &#37;1 &#61; &#40;Base.arrayref&#41;&#40;true, a, 2&#41;::ANY
└──      return &#37;1
</pre>


<p>This is one common way that type inference can breakdown. For example, even if the array is all numbers, we can still break inference:</p>


<pre class='hljl'>
<span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>Number</span><span class='hljl-p'>[</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-ni'>3</span><span class='hljl-p'>]</span><span class='hljl-t'>
</span><span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>q</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>4</span><span class='hljl-t'>
  </span><span class='hljl-n'>b</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-t'>
  </span><span class='hljl-n'>c</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-p'>],</span><span class='hljl-n'>a</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-n'>d</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>b</span><span class='hljl-p'>,</span><span class='hljl-n'>c</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>d</span><span class='hljl-p'>,</span><span class='hljl-n'>x</span><span class='hljl-p'>[</span><span class='hljl-ni'>2</span><span class='hljl-p'>])</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nd'>@code_warntype</span><span class='hljl-t'> </span><span class='hljl-nf'>q</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
Body::ANY
1 ─ &#37;1 &#61; &#40;Base.arrayref&#41;&#40;true, x, 1&#41;::NUMBER
│   &#37;2 &#61; π &#40;4, Core.Compiler.Const&#40;4, false&#41;&#41;
│   &#37;3 &#61; &#40;Main.WeaveSandBox0.f&#41;&#40;&#37;1, &#37;2&#41;::ANY
│   &#37;4 &#61; π &#40;2, Core.Compiler.Const&#40;2, false&#41;&#41;
│   &#37;5 &#61; &#40;Main.WeaveSandBox0.f&#41;&#40;&#37;4, &#37;3&#41;::ANY
│   &#37;6 &#61; &#40;Base.arrayref&#41;&#40;true, x, 2&#41;::NUMBER
│   &#37;7 &#61; &#40;Main.WeaveSandBox0.f&#41;&#40;&#37;5, &#37;6&#41;::ANY
└──      return &#37;7
</pre>


<p>Here the type inference algorithm quickly gives up and infers to <code>Any</code>, losing all specialization and automatically switching to Python-style runtime type checking.</p>
<h3>Type definitions</h3>
<h3>Value types and isbits</h3>
<p>In Julia, types which can fully inferred and which are composed of primitive or <code>isbits</code> types are value types. This means that, inside of an array, their values are the values of the type itself, and not a pointer to the values.</p>
<p>You can check if the type is a value type through <code>isbits</code>:</p>


<pre class='hljl'>
<span class='hljl-nf'>isbits</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
true
</pre>


<p>Note that a Julia <code>struct</code> which holds i<code>sbits</code> values is <code>isbits</code> as well, if it&#39;s fully inferred:</p>


<pre class='hljl'>
<span class='hljl-k'>struct</span><span class='hljl-t'> </span><span class='hljl-n'>MyComplex</span><span class='hljl-t'>
  </span><span class='hljl-n'>real</span><span class='hljl-oB'>::</span><span class='hljl-n'>Float64</span><span class='hljl-t'>
  </span><span class='hljl-n'>imag</span><span class='hljl-oB'>::</span><span class='hljl-n'>Float64</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nf'>isbits</span><span class='hljl-p'>(</span><span class='hljl-nf'>MyComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>))</span>
</pre>


<pre class="output">
true
</pre>


<p>We can see that the compiler knows how to use this efficiently since it knows that what comes out is always <code>Float64</code>:</p>


<pre class='hljl'>
<span class='hljl-n'>Base</span><span class='hljl-oB'>.:+</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>::</span><span class='hljl-n'>MyComplex</span><span class='hljl-p'>,</span><span class='hljl-n'>b</span><span class='hljl-oB'>::</span><span class='hljl-n'>MyComplex</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MyComplex</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>real</span><span class='hljl-oB'>+</span><span class='hljl-n'>b</span><span class='hljl-oB'>.</span><span class='hljl-n'>real</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>imag</span><span class='hljl-oB'>+</span><span class='hljl-n'>b</span><span class='hljl-oB'>.</span><span class='hljl-n'>imag</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>Base</span><span class='hljl-oB'>.:+</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>::</span><span class='hljl-n'>MyComplex</span><span class='hljl-p'>,</span><span class='hljl-n'>b</span><span class='hljl-oB'>::</span><span class='hljl-n'>Int</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MyComplex</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>real</span><span class='hljl-oB'>+</span><span class='hljl-n'>b</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>imag</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>Base</span><span class='hljl-oB'>.:+</span><span class='hljl-p'>(</span><span class='hljl-n'>b</span><span class='hljl-oB'>::</span><span class='hljl-n'>Int</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-oB'>::</span><span class='hljl-n'>MyComplex</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MyComplex</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>real</span><span class='hljl-oB'>+</span><span class='hljl-n'>b</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>imag</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-nf'>MyComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>),</span><span class='hljl-nf'>MyComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>))</span>
</pre>


<pre class="output">
Main.WeaveSandBox0.MyComplex&#40;8.0, 2.0&#41;
</pre>



<pre class='hljl'>
<span class='hljl-nd'>@code_warntype</span><span class='hljl-t'> </span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-nf'>MyComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>),</span><span class='hljl-nf'>MyComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>))</span>
</pre>


<pre class="output">
Body::Main.WeaveSandBox0.MyComplex
1 ─ &#37;1 &#61; &#40;Base.getfield&#41;&#40;x, :real&#41;::Float64
│   &#37;2 &#61; &#40;Base.add_float&#41;&#40;&#37;1, 4.0&#41;::Float64
│   &#37;3 &#61; &#40;Base.getfield&#41;&#40;x, :imag&#41;::Float64
│   &#37;4 &#61; &#40;Base.add_float&#41;&#40;&#37;2, 2.0&#41;::Float64
│   &#37;5 &#61; &#40;Base.getfield&#41;&#40;y, :real&#41;::Float64
│   &#37;6 &#61; &#40;Base.add_float&#41;&#40;&#37;4, &#37;5&#41;::Float64
│   &#37;7 &#61; &#40;Base.getfield&#41;&#40;y, :imag&#41;::Float64
│   &#37;8 &#61; &#40;Base.add_float&#41;&#40;&#37;3, &#37;7&#41;::Float64
│   &#37;9 &#61; &#37;new&#40;Main.WeaveSandBox0.MyComplex, &#37;6, &#37;8&#41;::Main.WeaveSandBox0.MyC
omplex
└──      return &#37;9
</pre>



<pre class='hljl'>
<span class='hljl-nd'>@code_llvm</span><span class='hljl-t'> </span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-nf'>MyComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>),</span><span class='hljl-nf'>MyComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>))</span>
</pre>


<pre class="output">
;  @ none:2 within &#96;g&#39;
; Function Attrs: uwtable
define void @julia_g_15620&#40;&#123; double, double &#125;* noalias nocapture sret, &#123; do
uble, double &#125; addrspace&#40;11&#41;* nocapture nonnull readonly dereferenceable&#40;16
&#41;, &#123; double, double &#125; addrspace&#40;11&#41;* nocapture nonnull readonly dereference
able&#40;16&#41;&#41; #0 &#123;
top:
;  @ none:4 within &#96;g&#39;
; ┌ @ none:1 within &#96;f&#39;
; │┌ @ none:1 within &#96;&#43;&#39;
; ││┌ @ sysimg.jl:18 within &#96;getproperty&#39;
     &#37;3 &#61; getelementptr inbounds &#123; double, double &#125;, &#123; double, double &#125; add
rspace&#40;11&#41;* &#37;1, i64 0, i32 0
; ││└
; ││ @ none:1 within &#96;&#43;&#39; @ promotion.jl:313 @ float.jl:395
    &#37;4 &#61; load double, double addrspace&#40;11&#41;* &#37;3, align 8
    &#37;5 &#61; fadd double &#37;4, 4.000000e&#43;00
; ││ @ none:1 within &#96;&#43;&#39;
; ││┌ @ sysimg.jl:18 within &#96;getproperty&#39;
     &#37;6 &#61; getelementptr inbounds &#123; double, double &#125;, &#123; double, double &#125; add
rspace&#40;11&#41;* &#37;1, i64 0, i32 1
; └└└
;  @ none:5 within &#96;g&#39;
; ┌ @ none:1 within &#96;f&#39;
; │┌ @ none:1 within &#96;&#43;&#39; @ promotion.jl:313 @ float.jl:395
    &#37;7 &#61; fadd double &#37;5, 2.000000e&#43;00
; └└
;  @ none:6 within &#96;g&#39;
; ┌ @ none:1 within &#96;f&#39;
; │┌ @ none:1 within &#96;&#43;&#39; @ float.jl:395
    &#37;8 &#61; load double, double addrspace&#40;11&#41;* &#37;6, align 8
    &#37;9 &#61; bitcast &#123; double, double &#125; addrspace&#40;11&#41;* &#37;2 to &lt;2 x double&gt; addrs
pace&#40;11&#41;*
    &#37;10 &#61; load &lt;2 x double&gt;, &lt;2 x double&gt; addrspace&#40;11&#41;* &#37;9, align 8
    &#37;11 &#61; insertelement &lt;2 x double&gt; undef, double &#37;7, i32 0
    &#37;12 &#61; insertelement &lt;2 x double&gt; &#37;11, double &#37;8, i32 1
    &#37;13 &#61; fadd &lt;2 x double&gt; &#37;10, &#37;12
; └└
  &#37;14 &#61; bitcast &#123; double, double &#125;* &#37;0 to &lt;2 x double&gt;*
  store &lt;2 x double&gt; &#37;13, &lt;2 x double&gt;* &#37;14, align 8
  ret void
&#125;
</pre>


<p>Note that the compiled code simply works directly on the <code>double</code> pieces. We can also make this be concrete without pre-specifying that the values always have to be <code>Float64</code> by using a type parameter.</p>


<pre class='hljl'>
<span class='hljl-k'>struct</span><span class='hljl-t'> </span><span class='hljl-nf'>MyParameterizedComplex</span><span class='hljl-p'>{</span><span class='hljl-n'>T</span><span class='hljl-p'>}</span><span class='hljl-t'>
  </span><span class='hljl-n'>real</span><span class='hljl-oB'>::</span><span class='hljl-n'>T</span><span class='hljl-t'>
  </span><span class='hljl-n'>imag</span><span class='hljl-oB'>::</span><span class='hljl-n'>T</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nf'>isbits</span><span class='hljl-p'>(</span><span class='hljl-nf'>MyParameterizedComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>))</span>
</pre>


<pre class="output">
true
</pre>


<p>Note that <code>MyParameterizedComplex&#123;T&#125;</code> is a concrete type for every <code>T</code>: it is a shorthand form for defining a whole family of types.</p>


<pre class='hljl'>
<span class='hljl-n'>Base</span><span class='hljl-oB'>.:+</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>::</span><span class='hljl-n'>MyParameterizedComplex</span><span class='hljl-p'>,</span><span class='hljl-n'>b</span><span class='hljl-oB'>::</span><span class='hljl-n'>MyParameterizedComplex</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MyParameterizedComplex</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>real</span><span class='hljl-oB'>+</span><span class='hljl-n'>b</span><span class='hljl-oB'>.</span><span class='hljl-n'>real</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>imag</span><span class='hljl-oB'>+</span><span class='hljl-n'>b</span><span class='hljl-oB'>.</span><span class='hljl-n'>imag</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>Base</span><span class='hljl-oB'>.:+</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>::</span><span class='hljl-n'>MyParameterizedComplex</span><span class='hljl-p'>,</span><span class='hljl-n'>b</span><span class='hljl-oB'>::</span><span class='hljl-n'>Int</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MyParameterizedComplex</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>real</span><span class='hljl-oB'>+</span><span class='hljl-n'>b</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>imag</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>Base</span><span class='hljl-oB'>.:+</span><span class='hljl-p'>(</span><span class='hljl-n'>b</span><span class='hljl-oB'>::</span><span class='hljl-n'>Int</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-oB'>::</span><span class='hljl-n'>MyParameterizedComplex</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MyParameterizedComplex</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>real</span><span class='hljl-oB'>+</span><span class='hljl-n'>b</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>imag</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-nf'>MyParameterizedComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>),</span><span class='hljl-nf'>MyParameterizedComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>))</span>
</pre>


<pre class="output">
Main.WeaveSandBox0.MyParameterizedComplex&#123;Float64&#125;&#40;8.0, 2.0&#41;
</pre>



<pre class='hljl'>
<span class='hljl-nd'>@code_warntype</span><span class='hljl-t'> </span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-nf'>MyParameterizedComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>),</span><span class='hljl-nf'>MyParameterizedComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>))</span>
</pre>


<pre class="output">
Body::Main.WeaveSandBox0.MyParameterizedComplex&#123;Float64&#125;
1 ─ &#37;1 &#61; &#40;Base.getfield&#41;&#40;x, :real&#41;::Float64
│   &#37;2 &#61; &#40;Base.add_float&#41;&#40;&#37;1, 4.0&#41;::Float64
│   &#37;3 &#61; &#40;Base.getfield&#41;&#40;x, :imag&#41;::Float64
│   &#37;4 &#61; &#40;Base.add_float&#41;&#40;&#37;2, 2.0&#41;::Float64
│   &#37;5 &#61; &#40;Base.getfield&#41;&#40;y, :real&#41;::Float64
│   &#37;6 &#61; &#40;Base.add_float&#41;&#40;&#37;4, &#37;5&#41;::Float64
│   &#37;7 &#61; &#40;Base.getfield&#41;&#40;y, :imag&#41;::Float64
│   &#37;8 &#61; &#40;Base.add_float&#41;&#40;&#37;3, &#37;7&#41;::Float64
│   &#37;9 &#61; &#37;new&#40;Main.WeaveSandBox0.MyParameterizedComplex&#123;Float64&#125;, &#37;6, &#37;8&#41;::
Main.WeaveSandBox0.MyParameterizedComplex&#123;Float64&#125;
└──      return &#37;9
</pre>


<p>See that this code also automatically works and compiles efficiently for <code>Float32</code> as well:</p>


<pre class='hljl'>
<span class='hljl-nd'>@code_warntype</span><span class='hljl-t'> </span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-nf'>MyParameterizedComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0f0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0f0</span><span class='hljl-p'>),</span><span class='hljl-nf'>MyParameterizedComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0f0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0f0</span><span class='hljl-p'>))</span>
</pre>


<pre class="output">
Body::Main.WeaveSandBox0.MyParameterizedComplex&#123;Float32&#125;
1 ─ &#37;1 &#61; &#40;Base.getfield&#41;&#40;x, :real&#41;::Float32
│   &#37;2 &#61; &#40;Base.add_float&#41;&#40;&#37;1, 4.0f0&#41;::Float32
│   &#37;3 &#61; &#40;Base.getfield&#41;&#40;x, :imag&#41;::Float32
│   &#37;4 &#61; &#40;Base.add_float&#41;&#40;&#37;2, 2.0f0&#41;::Float32
│   &#37;5 &#61; &#40;Base.getfield&#41;&#40;y, :real&#41;::Float32
│   &#37;6 &#61; &#40;Base.add_float&#41;&#40;&#37;4, &#37;5&#41;::Float32
│   &#37;7 &#61; &#40;Base.getfield&#41;&#40;y, :imag&#41;::Float32
│   &#37;8 &#61; &#40;Base.add_float&#41;&#40;&#37;3, &#37;7&#41;::Float32
│   &#37;9 &#61; &#37;new&#40;Main.WeaveSandBox0.MyParameterizedComplex&#123;Float32&#125;, &#37;6, &#37;8&#41;::
Main.WeaveSandBox0.MyParameterizedComplex&#123;Float32&#125;
└──      return &#37;9
</pre>



<pre class='hljl'>
<span class='hljl-nd'>@code_llvm</span><span class='hljl-t'> </span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-nf'>MyParameterizedComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0f0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0f0</span><span class='hljl-p'>),</span><span class='hljl-nf'>MyParameterizedComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0f0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0f0</span><span class='hljl-p'>))</span>
</pre>


<pre class="output">
;  @ none:2 within &#96;g&#39;
; Function Attrs: uwtable
define &#123; float, float &#125; @julia_g_15636&#40;&#123; float, float &#125; addrspace&#40;11&#41;* noca
pture nonnull readonly dereferenceable&#40;8&#41;, &#123; float, float &#125; addrspace&#40;11&#41;* 
nocapture nonnull readonly dereferenceable&#40;8&#41;&#41; #0 &#123;
top:
;  @ none:4 within &#96;g&#39;
; ┌ @ none:1 within &#96;f&#39;
; │┌ @ none:1 within &#96;&#43;&#39;
; ││┌ @ sysimg.jl:18 within &#96;getproperty&#39;
     &#37;2 &#61; getelementptr inbounds &#123; float, float &#125;, &#123; float, float &#125; addrspa
ce&#40;11&#41;* &#37;0, i64 0, i32 0
; ││└
; ││ @ none:1 within &#96;&#43;&#39; @ promotion.jl:313 @ float.jl:394
    &#37;3 &#61; load float, float addrspace&#40;11&#41;* &#37;2, align 4
    &#37;4 &#61; fadd float &#37;3, 4.000000e&#43;00
; ││ @ none:1 within &#96;&#43;&#39;
; ││┌ @ sysimg.jl:18 within &#96;getproperty&#39;
     &#37;5 &#61; getelementptr inbounds &#123; float, float &#125;, &#123; float, float &#125; addrspa
ce&#40;11&#41;* &#37;0, i64 0, i32 1
; └└└
;  @ none:5 within &#96;g&#39;
; ┌ @ none:1 within &#96;f&#39;
; │┌ @ none:1 within &#96;&#43;&#39; @ promotion.jl:313 @ float.jl:394
    &#37;6 &#61; fadd float &#37;4, 2.000000e&#43;00
; └└
;  @ none:6 within &#96;g&#39;
; ┌ @ none:1 within &#96;f&#39;
; │┌ @ none:1 within &#96;&#43;&#39;
; ││┌ @ sysimg.jl:18 within &#96;getproperty&#39;
     &#37;7 &#61; getelementptr inbounds &#123; float, float &#125;, &#123; float, float &#125; addrspa
ce&#40;11&#41;* &#37;1, i64 0, i32 0
; ││└
; ││ @ none:1 within &#96;&#43;&#39; @ float.jl:394
    &#37;8 &#61; load float, float addrspace&#40;11&#41;* &#37;7, align 4
    &#37;9 &#61; fadd float &#37;8, &#37;6
; ││ @ none:1 within &#96;&#43;&#39;
; ││┌ @ sysimg.jl:18 within &#96;getproperty&#39;
     &#37;10 &#61; getelementptr inbounds &#123; float, float &#125;, &#123; float, float &#125; addrsp
ace&#40;11&#41;* &#37;1, i64 0, i32 1
; ││└
; ││ @ none:1 within &#96;&#43;&#39; @ float.jl:394
    &#37;11 &#61; load float, float addrspace&#40;11&#41;* &#37;5, align 4
    &#37;12 &#61; load float, float addrspace&#40;11&#41;* &#37;10, align 4
    &#37;13 &#61; fadd float &#37;11, &#37;12
; └└
  &#37;.fca.0.insert &#61; insertvalue &#123; float, float &#125; undef, float &#37;9, 0
  &#37;.fca.1.insert &#61; insertvalue &#123; float, float &#125; &#37;.fca.0.insert, float &#37;13, 
1
  ret &#123; float, float &#125; &#37;.fca.1.insert
&#125;
</pre>


<p>It is important to know that if there is any piece of a type which doesn&#39;t contain type information, then it cannot be isbits because then it would have to be compiled in such a way that the size is not known in advance. For example:</p>


<pre class='hljl'>
<span class='hljl-k'>struct</span><span class='hljl-t'> </span><span class='hljl-n'>MySlowComplex</span><span class='hljl-t'>
  </span><span class='hljl-n'>real</span><span class='hljl-t'>
  </span><span class='hljl-n'>imag</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nf'>isbits</span><span class='hljl-p'>(</span><span class='hljl-nf'>MySlowComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>))</span>
</pre>


<pre class="output">
false
</pre>



<pre class='hljl'>
<span class='hljl-n'>Base</span><span class='hljl-oB'>.:+</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>::</span><span class='hljl-n'>MySlowComplex</span><span class='hljl-p'>,</span><span class='hljl-n'>b</span><span class='hljl-oB'>::</span><span class='hljl-n'>MySlowComplex</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MySlowComplex</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>real</span><span class='hljl-oB'>+</span><span class='hljl-n'>b</span><span class='hljl-oB'>.</span><span class='hljl-n'>real</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>imag</span><span class='hljl-oB'>+</span><span class='hljl-n'>b</span><span class='hljl-oB'>.</span><span class='hljl-n'>imag</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>Base</span><span class='hljl-oB'>.:+</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>::</span><span class='hljl-n'>MySlowComplex</span><span class='hljl-p'>,</span><span class='hljl-n'>b</span><span class='hljl-oB'>::</span><span class='hljl-n'>Int</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MySlowComplex</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>real</span><span class='hljl-oB'>+</span><span class='hljl-n'>b</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>imag</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>Base</span><span class='hljl-oB'>.:+</span><span class='hljl-p'>(</span><span class='hljl-n'>b</span><span class='hljl-oB'>::</span><span class='hljl-n'>Int</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-oB'>::</span><span class='hljl-n'>MySlowComplex</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MySlowComplex</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>real</span><span class='hljl-oB'>+</span><span class='hljl-n'>b</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>imag</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-nf'>MySlowComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>),</span><span class='hljl-nf'>MySlowComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>))</span>
</pre>


<pre class="output">
Main.WeaveSandBox0.MySlowComplex&#40;8.0, 2.0&#41;
</pre>



<pre class='hljl'>
<span class='hljl-nd'>@code_warntype</span><span class='hljl-t'> </span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-nf'>MySlowComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>),</span><span class='hljl-nf'>MySlowComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>))</span>
</pre>


<pre class="output">
Body::Main.WeaveSandBox0.MySlowComplex
1 ─ &#37;1 &#61; π &#40;4, Core.Compiler.Const&#40;4, false&#41;&#41;
│   &#37;2 &#61; invoke Main.WeaveSandBox0.:&#43;&#40;_2::Main.WeaveSandBox0.MySlowComplex,
 &#37;1::Int64&#41;::Main.WeaveSandBox0.MySlowComplex
│   &#37;3 &#61; π &#40;2, Core.Compiler.Const&#40;2, false&#41;&#41;
│   &#37;4 &#61; invoke Main.WeaveSandBox0.:&#43;&#40;&#37;3::Int64, &#37;2::Main.WeaveSandBox0.MyS
lowComplex&#41;::Main.WeaveSandBox0.MySlowComplex
│   &#37;5 &#61; invoke Main.WeaveSandBox0.:&#43;&#40;&#37;4::Main.WeaveSandBox0.MySlowComplex,
 _3::Main.WeaveSandBox0.MySlowComplex&#41;::Main.WeaveSandBox0.MySlowComplex
└──      return &#37;5
</pre>



<pre class='hljl'>
<span class='hljl-nd'>@code_llvm</span><span class='hljl-t'> </span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-nf'>MySlowComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>),</span><span class='hljl-nf'>MySlowComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>))</span>
</pre>


<pre class="output">
;  @ none:2 within &#96;g&#39;
; Function Attrs: uwtable
define nonnull &#37;jl_value_t addrspace&#40;10&#41;* @japi1_g_15638&#40;&#37;jl_value_t addrsp
ace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10&#41;**, i32&#41; #0 &#123;
top:
  &#37;3 &#61; alloca &#37;jl_value_t addrspace&#40;10&#41;*, i32 2
  &#37;gcframe &#61; alloca &#37;jl_value_t addrspace&#40;10&#41;*, i32 3
  &#37;4 &#61; bitcast &#37;jl_value_t addrspace&#40;10&#41;** &#37;gcframe to i8*
  call void @llvm.memset.p0i8.i32&#40;i8* &#37;4, i8 0, i32 24, i32 0, i1 false&#41;
  &#37;5 &#61; alloca &#37;jl_value_t addrspace&#40;10&#41;**, align 8
  store volatile &#37;jl_value_t addrspace&#40;10&#41;** &#37;1, &#37;jl_value_t addrspace&#40;10&#41;*
** &#37;5, align 8
  &#37;6 &#61; call &#37;jl_value_t*** inttoptr &#40;i64 18155952000 to &#37;jl_value_t*** &#40;&#41;*&#41;
&#40;&#41; #3
  &#37;7 &#61; getelementptr &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10&#41;*
* &#37;gcframe, i32 0
  &#37;8 &#61; bitcast &#37;jl_value_t addrspace&#40;10&#41;** &#37;7 to i64*
  store i64 2, i64* &#37;8
  &#37;9 &#61; getelementptr &#37;jl_value_t**, &#37;jl_value_t*** &#37;6, i32 0
  &#37;10 &#61; getelementptr &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10&#41;
** &#37;gcframe, i32 1
  &#37;11 &#61; bitcast &#37;jl_value_t addrspace&#40;10&#41;** &#37;10 to &#37;jl_value_t***
  &#37;12 &#61; load &#37;jl_value_t**, &#37;jl_value_t*** &#37;9
  store &#37;jl_value_t** &#37;12, &#37;jl_value_t*** &#37;11
  &#37;13 &#61; bitcast &#37;jl_value_t*** &#37;9 to &#37;jl_value_t addrspace&#40;10&#41;***
  store &#37;jl_value_t addrspace&#40;10&#41;** &#37;gcframe, &#37;jl_value_t addrspace&#40;10&#41;*** 
&#37;13
  &#37;14 &#61; load &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10&#41;** &#37;1, al
ign 8
  &#37;15 &#61; getelementptr inbounds &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addr
space&#40;10&#41;** &#37;1, i64 1
  &#37;16 &#61; load &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10&#41;** &#37;15, a
lign 8
;  @ none:4 within &#96;g&#39;
; ┌ @ none:1 within &#96;f&#39;
   &#37;17 &#61; call nonnull &#37;jl_value_t addrspace&#40;10&#41;* @&quot;julia_&#43;_15639&quot;&#40;&#37;jl_value
_t addrspace&#40;10&#41;* nonnull &#37;14, i64 4&#41;
   &#37;18 &#61; getelementptr &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10
&#41;** &#37;gcframe, i32 2
   store &#37;jl_value_t addrspace&#40;10&#41;* &#37;17, &#37;jl_value_t addrspace&#40;10&#41;** &#37;18
; └
;  @ none:5 within &#96;g&#39;
; ┌ @ none:1 within &#96;f&#39;
   &#37;19 &#61; call nonnull &#37;jl_value_t addrspace&#40;10&#41;* @&quot;julia_&#43;_15640&quot;&#40;i64 2, &#37;j
l_value_t addrspace&#40;10&#41;* nonnull &#37;17&#41;
   &#37;20 &#61; getelementptr &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10
&#41;** &#37;gcframe, i32 2
   store &#37;jl_value_t addrspace&#40;10&#41;* &#37;19, &#37;jl_value_t addrspace&#40;10&#41;** &#37;20
; └
;  @ none:6 within &#96;g&#39;
; ┌ @ none:1 within &#96;f&#39;
   &#37;21 &#61; getelementptr &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10
&#41;** &#37;3, i32 0
   store &#37;jl_value_t addrspace&#40;10&#41;* &#37;19, &#37;jl_value_t addrspace&#40;10&#41;** &#37;21
   &#37;22 &#61; getelementptr &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10
&#41;** &#37;3, i32 1
   store &#37;jl_value_t addrspace&#40;10&#41;* &#37;16, &#37;jl_value_t addrspace&#40;10&#41;** &#37;22
   &#37;23 &#61; call nonnull &#37;jl_value_t addrspace&#40;10&#41;* @&quot;japi1_&#43;_15641&quot;&#40;&#37;jl_value
_t addrspace&#40;10&#41;* addrspacecast &#40;&#37;jl_value_t* inttoptr &#40;i64 21697839088 to 
&#37;jl_value_t*&#41; to &#37;jl_value_t addrspace&#40;10&#41;*&#41;, &#37;jl_value_t addrspace&#40;10&#41;** &#37;
3, i32 2&#41;
; └
  &#37;24 &#61; getelementptr &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10&#41;
** &#37;gcframe, i32 1
  &#37;25 &#61; load &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10&#41;** &#37;24
  &#37;26 &#61; getelementptr &#37;jl_value_t**, &#37;jl_value_t*** &#37;6, i32 0
  &#37;27 &#61; bitcast &#37;jl_value_t*** &#37;26 to &#37;jl_value_t addrspace&#40;10&#41;**
  store &#37;jl_value_t addrspace&#40;10&#41;* &#37;25, &#37;jl_value_t addrspace&#40;10&#41;** &#37;27
  ret &#37;jl_value_t addrspace&#40;10&#41;* &#37;23
&#125;
</pre>



<pre class='hljl'>
<span class='hljl-k'>struct</span><span class='hljl-t'> </span><span class='hljl-n'>MySlowComplex2</span><span class='hljl-t'>
  </span><span class='hljl-n'>real</span><span class='hljl-oB'>::</span><span class='hljl-n'>AbstractFloat</span><span class='hljl-t'>
  </span><span class='hljl-n'>imag</span><span class='hljl-oB'>::</span><span class='hljl-n'>AbstractFloat</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nf'>isbits</span><span class='hljl-p'>(</span><span class='hljl-nf'>MySlowComplex2</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>))</span>
</pre>


<pre class="output">
false
</pre>



<pre class='hljl'>
<span class='hljl-n'>Base</span><span class='hljl-oB'>.:+</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>::</span><span class='hljl-n'>MySlowComplex2</span><span class='hljl-p'>,</span><span class='hljl-n'>b</span><span class='hljl-oB'>::</span><span class='hljl-n'>MySlowComplex2</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MySlowComplex2</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>real</span><span class='hljl-oB'>+</span><span class='hljl-n'>b</span><span class='hljl-oB'>.</span><span class='hljl-n'>real</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>imag</span><span class='hljl-oB'>+</span><span class='hljl-n'>b</span><span class='hljl-oB'>.</span><span class='hljl-n'>imag</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>Base</span><span class='hljl-oB'>.:+</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>::</span><span class='hljl-n'>MySlowComplex2</span><span class='hljl-p'>,</span><span class='hljl-n'>b</span><span class='hljl-oB'>::</span><span class='hljl-n'>Int</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MySlowComplex2</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>real</span><span class='hljl-oB'>+</span><span class='hljl-n'>b</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>imag</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>Base</span><span class='hljl-oB'>.:+</span><span class='hljl-p'>(</span><span class='hljl-n'>b</span><span class='hljl-oB'>::</span><span class='hljl-n'>Int</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-oB'>::</span><span class='hljl-n'>MySlowComplex2</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MySlowComplex2</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>real</span><span class='hljl-oB'>+</span><span class='hljl-n'>b</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-oB'>.</span><span class='hljl-n'>imag</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-nf'>MySlowComplex2</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>),</span><span class='hljl-nf'>MySlowComplex2</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>))</span>
</pre>


<pre class="output">
Main.WeaveSandBox0.MySlowComplex2&#40;8.0, 2.0&#41;
</pre>


<p>Here&#39;s the timings:</p>


<pre class='hljl'>
<span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MyComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>b</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MyComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-p'>,</span><span class='hljl-n'>b</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
17.635 ns &#40;1 allocation: 32 bytes&#41;
Main.WeaveSandBox0.MyComplex&#40;9.0, 2.0&#41;
</pre>



<pre class='hljl'>
<span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MyParameterizedComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>b</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MyParameterizedComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-p'>,</span><span class='hljl-n'>b</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
19.257 ns &#40;1 allocation: 32 bytes&#41;
Main.WeaveSandBox0.MyParameterizedComplex&#123;Float64&#125;&#40;9.0, 2.0&#41;
</pre>



<pre class='hljl'>
<span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MySlowComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>b</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MySlowComplex</span><span class='hljl-p'>(</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-p'>,</span><span class='hljl-n'>b</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
92.976 ns &#40;7 allocations: 160 bytes&#41;
Main.WeaveSandBox0.MySlowComplex&#40;9.0, 2.0&#41;
</pre>



<pre class='hljl'>
<span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MySlowComplex2</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-n'>b</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>MySlowComplex2</span><span class='hljl-p'>(</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>g</span><span class='hljl-p'>(</span><span class='hljl-n'>a</span><span class='hljl-p'>,</span><span class='hljl-n'>b</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
434.843 ns &#40;13 allocations: 256 bytes&#41;
Main.WeaveSandBox0.MySlowComplex2&#40;9.0, 2.0&#41;
</pre>


<h3>Note on Julia</h3>
<p>Note that, because of these type specialization, value types, etc. properties, the number types, even ones such as <code>Int</code>, <code>Float64</code>, and <code>Complex</code>, are all themselves implemented in pure Julia&#33; Thus even basic pieces can be implemented in Julia with full performance, given one uses the features correctly.</p>
<h3>Note on isbits</h3>
<p>Note that a type which is <code>mutable struct</code> will not be isbits. This means that mutable structs will be a pointer to a heap allocated object, unless it&#39;s shortlived and the compiler can erase its construction. Also, note that <code>isbits</code> compiles down to bit operations from pure Julia, which means that these types can directly compile to GPU kernels through CUDAnative without modification.</p>
<h3>Function Barriers</h3>
<p>Since functions automatically specialize on their input types in Julia, we can use this to our advantage in order to make an inner loop fully inferred. For example, take the code from above but with a loop:</p>


<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>r</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>4</span><span class='hljl-t'>
  </span><span class='hljl-n'>b</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-t'>
  </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>i</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-t'>
    </span><span class='hljl-n'>c</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-p'>],</span><span class='hljl-n'>a</span><span class='hljl-p'>)</span><span class='hljl-t'>
    </span><span class='hljl-n'>d</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>b</span><span class='hljl-p'>,</span><span class='hljl-n'>c</span><span class='hljl-p'>)</span><span class='hljl-t'>
    </span><span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>d</span><span class='hljl-p'>,</span><span class='hljl-n'>x</span><span class='hljl-p'>[</span><span class='hljl-ni'>2</span><span class='hljl-p'>])</span><span class='hljl-t'>
  </span><span class='hljl-k'>end</span><span class='hljl-t'>
  </span><span class='hljl-n'>a</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>r</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
4.186 μs &#40;300 allocations: 4.69 KiB&#41;
604.0
</pre>


<p>In here, the loop variables are not inferred and thus this is really slow. However, we can force a function call in the middle to end up with specialization and in the inner loop be stable:</p>


<pre class='hljl'>
<span class='hljl-nf'>s</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>_s</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-p'>],</span><span class='hljl-n'>x</span><span class='hljl-p'>[</span><span class='hljl-ni'>2</span><span class='hljl-p'>])</span><span class='hljl-t'>
</span><span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>_s</span><span class='hljl-p'>(</span><span class='hljl-n'>x1</span><span class='hljl-p'>,</span><span class='hljl-n'>x2</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>4</span><span class='hljl-t'>
  </span><span class='hljl-n'>b</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-t'>
  </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>i</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-t'>
    </span><span class='hljl-n'>c</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>x1</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-p'>)</span><span class='hljl-t'>
    </span><span class='hljl-n'>d</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>b</span><span class='hljl-p'>,</span><span class='hljl-n'>c</span><span class='hljl-p'>)</span><span class='hljl-t'>
    </span><span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>f</span><span class='hljl-p'>(</span><span class='hljl-n'>d</span><span class='hljl-p'>,</span><span class='hljl-n'>x2</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-k'>end</span><span class='hljl-t'>
  </span><span class='hljl-n'>a</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>s</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
306.611 ns &#40;1 allocation: 16 bytes&#41;
604.0
</pre>


<p>Notice that this algorithm still doesn&#39;t infer:</p>


<pre class='hljl'>
<span class='hljl-nd'>@code_warntype</span><span class='hljl-t'> </span><span class='hljl-nf'>s</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
Body::ANY
1 ─ &#37;1 &#61; &#40;Base.arrayref&#41;&#40;true, x, 1&#41;::NUMBER
│   &#37;2 &#61; &#40;Base.arrayref&#41;&#40;true, x, 2&#41;::NUMBER
│   &#37;3 &#61; &#40;Main.WeaveSandBox0._s&#41;&#40;&#37;1, &#37;2&#41;::ANY
└──      return &#37;3
</pre>


<p>since the output of <code>_s</code> isn&#39;t inferred, but while it&#39;s in <code>_s</code> it will have specialized on the fact that <code>x&#91;1&#93;</code> is a <code>Float64</code> while <code>x&#91;2&#93;</code> is a <code>Int</code>, making that inner loop fast. In fact, it will only need to pay one <em>dynamic dispatch</em>, i.e. a multiple dispatch determination that happens at runtime. Notice that whenever functions are inferred, the dispatching is static since the choice of the dispatch is already made and compiled into the LLVM IR.</p>
<h3>Specialization at Compile Time</h3>
<p>Julia code will specialize at compile time if it can prove something about the result. For example:</p>


<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>fff</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-k'>if</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-n'>isa</span><span class='hljl-t'> </span><span class='hljl-n'>Int</span><span class='hljl-t'>
    </span><span class='hljl-n'>y</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-t'>
  </span><span class='hljl-k'>else</span><span class='hljl-t'>
    </span><span class='hljl-n'>y</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nfB'>4.0</span><span class='hljl-t'>
  </span><span class='hljl-k'>end</span><span class='hljl-t'>
  </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>y</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span>
</pre>


<pre class="output">
fff &#40;generic function with 1 method&#41;
</pre>


<p>You might think this function has a branch, but in reality Julia can determine whether <code>x</code> is an <code>Int</code> or not at compile time, so it will actually compile it away and just turn it into the function <code>x&#43;2</code> or <code>x&#43;4.0</code>:</p>


<pre class='hljl'>
<span class='hljl-nd'>@code_llvm</span><span class='hljl-t'> </span><span class='hljl-nf'>fff</span><span class='hljl-p'>(</span><span class='hljl-ni'>5</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
;  @ none:2 within &#96;fff&#39;
; Function Attrs: uwtable
define i64 @julia_fff_15764&#40;i64&#41; #0 &#123;
top:
;  @ none:7 within &#96;fff&#39;
; ┌ @ int.jl:53 within &#96;&#43;&#39;
   &#37;1 &#61; add i64 &#37;0, 2
; └
  ret i64 &#37;1
&#125;
</pre>



<pre class='hljl'>
<span class='hljl-nd'>@code_llvm</span><span class='hljl-t'> </span><span class='hljl-nf'>fff</span><span class='hljl-p'>(</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
;  @ none:2 within &#96;fff&#39;
; Function Attrs: uwtable
define double @julia_fff_15765&#40;double&#41; #0 &#123;
top:
;  @ none:7 within &#96;fff&#39;
; ┌ @ float.jl:395 within &#96;&#43;&#39;
   &#37;1 &#61; fadd double &#37;0, 4.000000e&#43;00
; └
  ret double &#37;1
&#125;
</pre>


<p>Thus one does not need to worry about over-optimizing since in the obvious cases the compiler will actually remove all of the extra pieces when it can&#33;</p>
<h3>Global Scope and Optimizations</h3>
<p>This discussion shows how Julia&#39;s optimizations all apply during function specialization times. Thus calling Julia functions is fast. But what about when doing something outside of the function, like directly in a module or in the REPL?</p>


<pre class='hljl'>
<span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>j</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>i</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-t'>
  </span><span class='hljl-kd'>global</span><span class='hljl-t'> </span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>,</span><span class='hljl-n'>C</span><span class='hljl-t'>
  </span><span class='hljl-n'>C</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>A</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>B</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span>
</pre>


<pre class="output">
748.800 μs &#40;30000 allocations: 468.75 KiB&#41;
</pre>


<p>This is very slow because the types of <code>A</code>, <code>B</code>, and <code>C</code> cannot be inferred. Why can&#39;t they be inferred? Well, at any time in the dynamic REPL scope I can do something like <code>C &#61; &quot;haha now a string&#33;&quot;</code>, and thus it cannot specialize on the types currently existing in the REPL &#40;since asynchronous changes could also occur&#41;, and therefore it defaults back to doing a type check at every single function which slows it down. Moral of the story, Julia functions are fast but its global scope is too dynamic to be optimized.</p>
<h3>Summary</h3>
<ul>
<li><p>Julia is not fast because of its JIT, it&#39;s fast because of function specialization and type inference</p>
</li>
<li><p>Type stable functions allow inference to fully occur</p>
</li>
<li><p>Multiple dispatch works within the function specialization mechanism to create overhead-free compile time controls</p>
</li>
<li><p>Julia will specialize the generic functions</p>
</li>
<li><p>Making sure values are concretely typed in inner loops is essential for performance</p>
</li>
</ul>
<h2>Overheads of Individual Operations</h2>
<p>Now let&#39;s dig even a little deeper. Everything the processor does has a cost. A great chart to keep in mind is <a href="http://ithare.com/infographics-operation-costs-in-cpu-clock-cycles/">this classic one</a>. A few things should immediately jump out to you:</p>
<ul>
<li><p>Simple arithmetic, like floating point additions, are super cheap. ~1 clock cycle, or a few nanoseconds.</p>
</li>
<li><p>Processors do <em>branch prediction</em> on <code>if</code> statements. If the code goes down the predicted route, the <code>if</code> statement costs ~1-2 clock cycles. If it goes down the wrong route, then it will take ~10-20 clock cycles. This means that predictable branches, like ones with clear patterns or usually the same output, are much cheaper &#40;almost free&#41; than unpredictable branches.</p>
</li>
<li><p>Function calls are expensive: 15-60 clock cycles&#33;</p>
</li>
<li><p>RAM reads are very expensive, with lower caches less expensive.</p>
</li>
</ul>
<h3>Bounds Checking</h3>
<p>Let&#39;s check the LLVM IR on one of our earlier loops:</p>


<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>inner_noalloc!</span><span class='hljl-p'>(</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>j</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>i</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-t'>
    </span><span class='hljl-n'>val</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>A</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>B</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'>
    </span><span class='hljl-n'>C</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>val</span><span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-p'>]</span><span class='hljl-t'>
  </span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nd'>@code_llvm</span><span class='hljl-t'> </span><span class='hljl-nf'>inner_noalloc!</span><span class='hljl-p'>(</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
;  @ none:2 within &#96;inner_noalloc&#33;&#39;
; Function Attrs: uwtable
define nonnull &#37;jl_value_t addrspace&#40;10&#41;* @&quot;japi1_inner_noalloc&#33;_15782&quot;&#40;&#37;jl
_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10&#41;**, i32&#41; #0 &#123;
top:
  &#37;3 &#61; alloca &#37;jl_value_t addrspace&#40;10&#41;**, align 8
  store volatile &#37;jl_value_t addrspace&#40;10&#41;** &#37;1, &#37;jl_value_t addrspace&#40;10&#41;*
** &#37;3, align 8
  &#37;4 &#61; getelementptr inbounds &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrs
pace&#40;10&#41;** &#37;1, i64 1
  &#37;5 &#61; load &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10&#41;** &#37;4, ali
gn 8
;  @ none:3 within &#96;inner_noalloc&#33;&#39;
; ┌ @ array.jl:730 within &#96;getindex&#39;
   &#37;6 &#61; addrspacecast &#37;jl_value_t addrspace&#40;10&#41;* &#37;5 to &#37;jl_value_t addrspac
e&#40;11&#41;*
   &#37;7 &#61; bitcast &#37;jl_value_t addrspace&#40;11&#41;* &#37;6 to &#37;jl_value_t addrspace&#40;10&#41;*
 addrspace&#40;11&#41;*
   &#37;8 &#61; getelementptr inbounds &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addr
space&#40;10&#41;* addrspace&#40;11&#41;* &#37;7, i64 3
   &#37;9 &#61; bitcast &#37;jl_value_t addrspace&#40;10&#41;* addrspace&#40;11&#41;* &#37;8 to i64 addrspa
ce&#40;11&#41;*
   &#37;10 &#61; load i64, i64 addrspace&#40;11&#41;* &#37;9, align 8
   &#37;11 &#61; icmp eq i64 &#37;10, 0
   br i1 &#37;11, label &#37;oob, label &#37;ib.lr.ph.lr.ph.split.us

ib.lr.ph.lr.ph.split.us:                          ; preds &#61; &#37;top
   &#37;12 &#61; load &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10&#41;** &#37;1, a
lign 8
   &#37;13 &#61; getelementptr inbounds &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t add
rspace&#40;10&#41;** &#37;1, i64 2
   &#37;14 &#61; load &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10&#41;** &#37;13, 
align 8
   &#37;15 &#61; getelementptr inbounds &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t add
rspace&#40;10&#41;* addrspace&#40;11&#41;* &#37;7, i64 4
   &#37;16 &#61; bitcast &#37;jl_value_t addrspace&#40;10&#41;* addrspace&#40;11&#41;* &#37;15 to i64 addrs
pace&#40;11&#41;*
   &#37;17 &#61; load i64, i64 addrspace&#40;11&#41;* &#37;16, align 8
   &#37;18 &#61; bitcast &#37;jl_value_t addrspace&#40;11&#41;* &#37;6 to double addrspace&#40;13&#41;* add
rspace&#40;11&#41;*
   &#37;19 &#61; load double addrspace&#40;13&#41;*, double addrspace&#40;13&#41;* addrspace&#40;11&#41;* &#37;
18, align 8
   &#37;20 &#61; addrspacecast &#37;jl_value_t addrspace&#40;10&#41;* &#37;14 to &#37;jl_value_t addrsp
ace&#40;11&#41;*
   &#37;21 &#61; bitcast &#37;jl_value_t addrspace&#40;11&#41;* &#37;20 to &#37;jl_value_t addrspace&#40;10
&#41;* addrspace&#40;11&#41;*
   &#37;22 &#61; getelementptr inbounds &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t add
rspace&#40;10&#41;* addrspace&#40;11&#41;* &#37;21, i64 3
   &#37;23 &#61; bitcast &#37;jl_value_t addrspace&#40;10&#41;* addrspace&#40;11&#41;* &#37;22 to i64 addrs
pace&#40;11&#41;*
   &#37;24 &#61; load i64, i64 addrspace&#40;11&#41;* &#37;23, align 8
   &#37;25 &#61; getelementptr inbounds &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t add
rspace&#40;10&#41;* addrspace&#40;11&#41;* &#37;21, i64 4
   &#37;26 &#61; bitcast &#37;jl_value_t addrspace&#40;10&#41;* addrspace&#40;11&#41;* &#37;25 to i64 addrs
pace&#40;11&#41;*
   &#37;27 &#61; load i64, i64 addrspace&#40;11&#41;* &#37;26, align 8
   &#37;28 &#61; bitcast &#37;jl_value_t addrspace&#40;11&#41;* &#37;20 to double addrspace&#40;13&#41;* ad
drspace&#40;11&#41;*
   &#37;29 &#61; load double addrspace&#40;13&#41;*, double addrspace&#40;13&#41;* addrspace&#40;11&#41;* &#37;
28, align 8
   &#37;30 &#61; addrspacecast &#37;jl_value_t addrspace&#40;10&#41;* &#37;12 to &#37;jl_value_t addrsp
ace&#40;11&#41;*
   &#37;31 &#61; bitcast &#37;jl_value_t addrspace&#40;11&#41;* &#37;30 to &#37;jl_value_t addrspace&#40;10
&#41;* addrspace&#40;11&#41;*
   &#37;32 &#61; getelementptr inbounds &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t add
rspace&#40;10&#41;* addrspace&#40;11&#41;* &#37;31, i64 3
   &#37;33 &#61; bitcast &#37;jl_value_t addrspace&#40;10&#41;* addrspace&#40;11&#41;* &#37;32 to i64 addrs
pace&#40;11&#41;*
   &#37;34 &#61; load i64, i64 addrspace&#40;11&#41;* &#37;33, align 8
   &#37;35 &#61; getelementptr inbounds &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t add
rspace&#40;10&#41;* addrspace&#40;11&#41;* &#37;31, i64 4
   &#37;36 &#61; bitcast &#37;jl_value_t addrspace&#40;10&#41;* addrspace&#40;11&#41;* &#37;35 to i64 addrs
pace&#40;11&#41;*
   &#37;37 &#61; load i64, i64 addrspace&#40;11&#41;* &#37;36, align 8
   &#37;38 &#61; bitcast &#37;jl_value_t addrspace&#40;11&#41;* &#37;30 to double addrspace&#40;13&#41;* ad
drspace&#40;11&#41;*
   &#37;39 &#61; load double addrspace&#40;13&#41;*, double addrspace&#40;13&#41;* addrspace&#40;11&#41;* &#37;
38, align 8
   br label &#37;ib.lr.ph.us

ib.lr.ph.us:                                      ; preds &#61; &#37;L35.us, &#37;ib.lr
.ph.lr.ph.split.us
   &#37;value_phi30.us &#61; phi i64 &#91; 1, &#37;ib.lr.ph.lr.ph.split.us &#93;, &#91; &#37;44, &#37;L35.u
s &#93;
   &#37;40 &#61; add i64 &#37;value_phi30.us, -1
   &#37;41 &#61; icmp ult i64 &#37;40, &#37;17
   &#37;42 &#61; icmp ult i64 &#37;40, &#37;37
   br i1 &#37;41, label &#37;ib.lr.ph.split.us.us, label &#37;oob

L25.us:                                           ; preds &#61; &#37;idxend9.us.us.
us.us
; └
;  @ none:4 within &#96;inner_noalloc&#33;&#39;
; ┌ @ range.jl:594 within &#96;iterate&#39;
; │┌ @ promotion.jl:403 within &#96;&#61;&#61;&#39;
    &#37;43 &#61; icmp eq i64 &#37;value_phi30.us, 100
; └└
  br i1 &#37;43, label &#37;L36, label &#37;L35.us

L35.us:                                           ; preds &#61; &#37;L25.us
; ┌ @ range.jl:595 within &#96;iterate&#39;
; │┌ @ int.jl:53 within &#96;&#43;&#39;
    &#37;44 &#61; add i64 &#37;value_phi30.us, 1
; └└
;  @ none:3 within &#96;inner_noalloc&#33;&#39;
; ┌ @ array.jl:730 within &#96;getindex&#39;
   br label &#37;ib.lr.ph.us

ib.lr.ph.split.us.us:                             ; preds &#61; &#37;ib.lr.ph.us
   &#37;45 &#61; icmp ult i64 &#37;40, &#37;27
   br i1 &#37;45, label &#37;ib.lr.ph.split.us.split.us.us, label &#37;oob5

idxend.us.us.us:                                  ; preds &#61; &#37;ib.lr.ph.split
.us.split.us.us
   &#37;46 &#61; icmp eq i64 &#37;24, 0
   br i1 &#37;46, label &#37;oob5, label &#37;oob8

ib.lr.ph.split.us.split.us.us:                    ; preds &#61; &#37;ib.lr.ph.split
.us.us
   br i1 &#37;42, label &#37;ib.lr.ph.split.us.split.us.us.split.us, label &#37;idxend.
us.us.us

ib.lr.ph.split.us.split.us.us.split.us:           ; preds &#61; &#37;ib.lr.ph.split
.us.split.us.us
   &#37;47 &#61; mul i64 &#37;24, &#37;40
   &#37;48 &#61; mul i64 &#37;34, &#37;40
   br label &#37;idxend.us.us.us.us

idxend.us.us.us.us:                               ; preds &#61; &#37;ib.lr.ph.split
.us.split.us.us.split.us, &#37;L24.us.us.us.us
   &#37;49 &#61; phi i64 &#91; 0, &#37;ib.lr.ph.split.us.split.us.us.split.us &#93;, &#91; &#37;value_p
hi224.us.us.us.us, &#37;L24.us.us.us.us &#93;
   &#37;value_phi224.us.us.us.us &#61; phi i64 &#91; 1, &#37;ib.lr.ph.split.us.split.us.us.
split.us &#93;, &#91; &#37;63, &#37;L24.us.us.us.us &#93;
   &#37;50 &#61; icmp ult i64 &#37;49, &#37;24
   br i1 &#37;50, label &#37;idxend6.us.us.us.us, label &#37;oob5

idxend6.us.us.us.us:                              ; preds &#61; &#37;idxend.us.us.u
s.us
; └
;  @ none:4 within &#96;inner_noalloc&#33;&#39;
; ┌ @ array.jl:769 within &#96;setindex&#33;&#39;
   &#37;51 &#61; icmp ult i64 &#37;49, &#37;34
   br i1 &#37;51, label &#37;idxend9.us.us.us.us, label &#37;oob8

idxend9.us.us.us.us:                              ; preds &#61; &#37;idxend6.us.us.
us.us
; └
;  @ none:3 within &#96;inner_noalloc&#33;&#39;
; ┌ @ array.jl:730 within &#96;getindex&#39;
   &#37;52 &#61; mul i64 &#37;10, &#37;40
   &#37;53 &#61; add i64 &#37;49, &#37;52
   &#37;54 &#61; getelementptr inbounds double, double addrspace&#40;13&#41;* &#37;19, i64 &#37;53
   &#37;55 &#61; load double, double addrspace&#40;13&#41;* &#37;54, align 8
   &#37;56 &#61; add i64 &#37;49, &#37;47
   &#37;57 &#61; getelementptr inbounds double, double addrspace&#40;13&#41;* &#37;29, i64 &#37;56
   &#37;58 &#61; load double, double addrspace&#40;13&#41;* &#37;57, align 8
; └
; ┌ @ float.jl:395 within &#96;&#43;&#39;
   &#37;59 &#61; fadd double &#37;55, &#37;58
; └
;  @ none:4 within &#96;inner_noalloc&#33;&#39;
; ┌ @ array.jl:769 within &#96;setindex&#33;&#39;
   &#37;60 &#61; add i64 &#37;49, &#37;48
   &#37;61 &#61; getelementptr inbounds double, double addrspace&#40;13&#41;* &#37;39, i64 &#37;60
   store double &#37;59, double addrspace&#40;13&#41;* &#37;61, align 8
; └
; ┌ @ range.jl:594 within &#96;iterate&#39;
; │┌ @ promotion.jl:403 within &#96;&#61;&#61;&#39;
    &#37;62 &#61; icmp eq i64 &#37;value_phi224.us.us.us.us, 100
; └└
  br i1 &#37;62, label &#37;L25.us, label &#37;L24.us.us.us.us

L24.us.us.us.us:                                  ; preds &#61; &#37;idxend9.us.us.
us.us
; ┌ @ range.jl:595 within &#96;iterate&#39;
; │┌ @ int.jl:53 within &#96;&#43;&#39;
    &#37;63 &#61; add nuw nsw i64 &#37;value_phi224.us.us.us.us, 1
; └└
;  @ none:3 within &#96;inner_noalloc&#33;&#39;
; ┌ @ array.jl:730 within &#96;getindex&#39;
   &#37;64 &#61; icmp ult i64 &#37;value_phi224.us.us.us.us, &#37;10
   br i1 &#37;64, label &#37;idxend.us.us.us.us, label &#37;oob

L36:                                              ; preds &#61; &#37;L25.us
; └
;  @ none:4 within &#96;inner_noalloc&#33;&#39;
  ret &#37;jl_value_t addrspace&#40;10&#41;* addrspacecast &#40;&#37;jl_value_t* inttoptr &#40;i64 
87752712 to &#37;jl_value_t*&#41; to &#37;jl_value_t addrspace&#40;10&#41;*&#41;

oob:                                              ; preds &#61; &#37;ib.lr.ph.us, &#37;
L24.us.us.us.us, &#37;top
  &#37;value_phi.lcssa &#61; phi i64 &#91; 1, &#37;top &#93;, &#91; &#37;value_phi30.us, &#37;L24.us.us.us.
us &#93;, &#91; &#37;value_phi30.us, &#37;ib.lr.ph.us &#93;
  &#37;value_phi2.lcssa &#61; phi i64 &#91; 1, &#37;top &#93;, &#91; &#37;63, &#37;L24.us.us.us.us &#93;, &#91; 1, 
&#37;ib.lr.ph.us &#93;
;  @ none:3 within &#96;inner_noalloc&#33;&#39;
; ┌ @ array.jl:730 within &#96;getindex&#39;
   &#37;65 &#61; alloca &#91;2 x i64&#93;, align 8
   &#37;.sub &#61; getelementptr inbounds &#91;2 x i64&#93;, &#91;2 x i64&#93;* &#37;65, i64 0, i64 0
   store i64 &#37;value_phi2.lcssa, i64* &#37;.sub, align 8
   &#37;66 &#61; getelementptr inbounds &#91;2 x i64&#93;, &#91;2 x i64&#93;* &#37;65, i64 0, i64 1
   store i64 &#37;value_phi.lcssa, i64* &#37;66, align 8
   &#37;67 &#61; addrspacecast &#37;jl_value_t addrspace&#40;10&#41;* &#37;5 to &#37;jl_value_t addrspa
ce&#40;12&#41;*
   call void @jl_bounds_error_ints&#40;&#37;jl_value_t addrspace&#40;12&#41;* &#37;67, i64* non
null &#37;.sub, i64 2&#41;
   unreachable

oob5:                                             ; preds &#61; &#37;ib.lr.ph.split
.us.us, &#37;idxend.us.us.us.us, &#37;idxend.us.us.us
   &#37;value_phi2.lcssa18.ph.us &#61; phi i64 &#91; 1, &#37;idxend.us.us.us &#93;, &#91; &#37;value_ph
i224.us.us.us.us, &#37;idxend.us.us.us.us &#93;, &#91; 1, &#37;ib.lr.ph.split.us.us &#93;
   &#37;68 &#61; alloca &#91;2 x i64&#93;, align 8
   &#37;.sub16 &#61; getelementptr inbounds &#91;2 x i64&#93;, &#91;2 x i64&#93;* &#37;68, i64 0, i64 0
   store i64 &#37;value_phi2.lcssa18.ph.us, i64* &#37;.sub16, align 8
   &#37;69 &#61; getelementptr inbounds &#91;2 x i64&#93;, &#91;2 x i64&#93;* &#37;68, i64 0, i64 1
   store i64 &#37;value_phi30.us, i64* &#37;69, align 8
   &#37;70 &#61; addrspacecast &#37;jl_value_t addrspace&#40;10&#41;* &#37;14 to &#37;jl_value_t addrsp
ace&#40;12&#41;*
   call void @jl_bounds_error_ints&#40;&#37;jl_value_t addrspace&#40;12&#41;* &#37;70, i64* non
null &#37;.sub16, i64 2&#41;
   unreachable

oob8:                                             ; preds &#61; &#37;idxend6.us.us.
us.us, &#37;idxend.us.us.us
   &#37;value_phi2.lcssa19.ph.us.ph.us.ph.us &#61; phi i64 &#91; 1, &#37;idxend.us.us.us &#93;,
 &#91; &#37;value_phi224.us.us.us.us, &#37;idxend6.us.us.us.us &#93;
; └
;  @ none:4 within &#96;inner_noalloc&#33;&#39;
; ┌ @ array.jl:769 within &#96;setindex&#33;&#39;
   &#37;71 &#61; alloca &#91;2 x i64&#93;, align 8
   &#37;.sub17 &#61; getelementptr inbounds &#91;2 x i64&#93;, &#91;2 x i64&#93;* &#37;71, i64 0, i64 0
   store i64 &#37;value_phi2.lcssa19.ph.us.ph.us.ph.us, i64* &#37;.sub17, align 8
   &#37;72 &#61; getelementptr inbounds &#91;2 x i64&#93;, &#91;2 x i64&#93;* &#37;71, i64 0, i64 1
   store i64 &#37;value_phi30.us, i64* &#37;72, align 8
   &#37;73 &#61; addrspacecast &#37;jl_value_t addrspace&#40;10&#41;* &#37;12 to &#37;jl_value_t addrsp
ace&#40;12&#41;*
   call void @jl_bounds_error_ints&#40;&#37;jl_value_t addrspace&#40;12&#41;* &#37;73, i64* non
null &#37;.sub17, i64 2&#41;
   unreachable
; └
&#125;
</pre>


<p>Notice that this <code>getelementptr inbounds</code> stuff is bounds checking. Julia, like all other high level languages, enables bounds checking by default in order to not allow the user to index outside of an array. Indexing outside of an array is dangerous: it can quite easily segfault your system if you change some memory that is unknown beyond your actual array. Thus Julia throws an error:</p>


<pre class='hljl'>
<span class='hljl-n'>A</span><span class='hljl-p'>[</span><span class='hljl-ni'>101</span><span class='hljl-p'>,</span><span class='hljl-ni'>1</span><span class='hljl-p'>]</span>
</pre>


<pre class="julia-error">
ERROR: BoundsError: attempt to access 100×100 Array&#123;Float64,2&#125; at index &#91;101, 1&#93;
</pre>


<p>In tight inner loops, we can remove this bounds checking process using the <code>@inbounds</code> macro:</p>


<pre class='hljl'>
<span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>inner_noalloc_ib!</span><span class='hljl-p'>(</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-nd'>@inbounds</span><span class='hljl-t'> </span><span class='hljl-k'>for</span><span class='hljl-t'> </span><span class='hljl-n'>j</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-p'>,</span><span class='hljl-t'> </span><span class='hljl-n'>i</span><span class='hljl-t'> </span><span class='hljl-kp'>in</span><span class='hljl-t'> </span><span class='hljl-ni'>1</span><span class='hljl-oB'>:</span><span class='hljl-ni'>100</span><span class='hljl-t'>
    </span><span class='hljl-n'>val</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>A</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>B</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'>
    </span><span class='hljl-n'>C</span><span class='hljl-p'>[</span><span class='hljl-n'>i</span><span class='hljl-p'>,</span><span class='hljl-n'>j</span><span class='hljl-p'>]</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>val</span><span class='hljl-p'>[</span><span class='hljl-ni'>1</span><span class='hljl-p'>]</span><span class='hljl-t'>
  </span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>inner_noalloc!</span><span class='hljl-p'>(</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
6.520 μs &#40;0 allocations: 0 bytes&#41;
</pre>



<pre class='hljl'>
<span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>inner_noalloc_ib!</span><span class='hljl-p'>(</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
2.156 μs &#40;0 allocations: 0 bytes&#41;
</pre>


<h3>SIMD</h3>
<p>Now let&#39;s inspect the LLVM IR again:</p>


<pre class='hljl'>
<span class='hljl-nd'>@code_llvm</span><span class='hljl-t'> </span><span class='hljl-nf'>inner_noalloc_ib!</span><span class='hljl-p'>(</span><span class='hljl-n'>C</span><span class='hljl-p'>,</span><span class='hljl-n'>A</span><span class='hljl-p'>,</span><span class='hljl-n'>B</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
;  @ none:2 within &#96;inner_noalloc_ib&#33;&#39;
; Function Attrs: uwtable
define nonnull &#37;jl_value_t addrspace&#40;10&#41;* @&quot;japi1_inner_noalloc_ib&#33;_15814&quot;&#40;
&#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10&#41;**, i32&#41; #0 &#123;
top:
  &#37;3 &#61; alloca &#37;jl_value_t addrspace&#40;10&#41;**, align 8
  store volatile &#37;jl_value_t addrspace&#40;10&#41;** &#37;1, &#37;jl_value_t addrspace&#40;10&#41;*
** &#37;3, align 8
  &#37;4 &#61; load &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10&#41;** &#37;1, ali
gn 8
  &#37;5 &#61; getelementptr inbounds &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrs
pace&#40;10&#41;** &#37;1, i64 1
  &#37;6 &#61; load &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10&#41;** &#37;5, ali
gn 8
  &#37;7 &#61; getelementptr inbounds &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrs
pace&#40;10&#41;** &#37;1, i64 2
  &#37;8 &#61; load &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addrspace&#40;10&#41;** &#37;7, ali
gn 8
  &#37;9 &#61; addrspacecast &#37;jl_value_t addrspace&#40;10&#41;* &#37;6 to &#37;jl_value_t addrspace
&#40;11&#41;*
  &#37;10 &#61; bitcast &#37;jl_value_t addrspace&#40;11&#41;* &#37;9 to &#37;jl_value_t addrspace&#40;10&#41;*
 addrspace&#40;11&#41;*
  &#37;11 &#61; getelementptr inbounds &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addr
space&#40;10&#41;* addrspace&#40;11&#41;* &#37;10, i64 3
  &#37;12 &#61; bitcast &#37;jl_value_t addrspace&#40;10&#41;* addrspace&#40;11&#41;* &#37;11 to i64 addrsp
ace&#40;11&#41;*
  &#37;13 &#61; load i64, i64 addrspace&#40;11&#41;* &#37;12, align 8
  &#37;14 &#61; bitcast &#37;jl_value_t addrspace&#40;11&#41;* &#37;9 to double addrspace&#40;13&#41;* addr
space&#40;11&#41;*
  &#37;15 &#61; load double addrspace&#40;13&#41;*, double addrspace&#40;13&#41;* addrspace&#40;11&#41;* &#37;1
4, align 8
  &#37;16 &#61; addrspacecast &#37;jl_value_t addrspace&#40;10&#41;* &#37;8 to &#37;jl_value_t addrspac
e&#40;11&#41;*
  &#37;17 &#61; bitcast &#37;jl_value_t addrspace&#40;11&#41;* &#37;16 to &#37;jl_value_t addrspace&#40;10&#41;
* addrspace&#40;11&#41;*
  &#37;18 &#61; getelementptr inbounds &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addr
space&#40;10&#41;* addrspace&#40;11&#41;* &#37;17, i64 3
  &#37;19 &#61; bitcast &#37;jl_value_t addrspace&#40;10&#41;* addrspace&#40;11&#41;* &#37;18 to i64 addrsp
ace&#40;11&#41;*
  &#37;20 &#61; load i64, i64 addrspace&#40;11&#41;* &#37;19, align 8
  &#37;21 &#61; bitcast &#37;jl_value_t addrspace&#40;11&#41;* &#37;16 to double addrspace&#40;13&#41;* add
rspace&#40;11&#41;*
  &#37;22 &#61; load double addrspace&#40;13&#41;*, double addrspace&#40;13&#41;* addrspace&#40;11&#41;* &#37;2
1, align 8
  &#37;23 &#61; addrspacecast &#37;jl_value_t addrspace&#40;10&#41;* &#37;4 to &#37;jl_value_t addrspac
e&#40;11&#41;*
  &#37;24 &#61; bitcast &#37;jl_value_t addrspace&#40;11&#41;* &#37;23 to &#37;jl_value_t addrspace&#40;10&#41;
* addrspace&#40;11&#41;*
  &#37;25 &#61; getelementptr inbounds &#37;jl_value_t addrspace&#40;10&#41;*, &#37;jl_value_t addr
space&#40;10&#41;* addrspace&#40;11&#41;* &#37;24, i64 3
  &#37;26 &#61; bitcast &#37;jl_value_t addrspace&#40;10&#41;* addrspace&#40;11&#41;* &#37;25 to i64 addrsp
ace&#40;11&#41;*
  &#37;27 &#61; load i64, i64 addrspace&#40;11&#41;* &#37;26, align 8
  &#37;28 &#61; bitcast &#37;jl_value_t addrspace&#40;11&#41;* &#37;23 to double addrspace&#40;13&#41;* add
rspace&#40;11&#41;*
  &#37;29 &#61; load double addrspace&#40;13&#41;*, double addrspace&#40;13&#41;* addrspace&#40;11&#41;* &#37;2
8, align 8
  br label &#37;L2

L2:                                               ; preds &#61; &#37;L25, &#37;top
  &#37;indvar &#61; phi i64 &#91; &#37;indvar.next, &#37;L25 &#93;, &#91; 0, &#37;top &#93;
  &#37;value_phi &#61; phi i64 &#91; &#37;65, &#37;L25 &#93;, &#91; 1, &#37;top &#93;
  &#37;30 &#61; mul i64 &#37;27, &#37;indvar
  &#37;scevgep &#61; getelementptr double, double addrspace&#40;13&#41;* &#37;29, i64 &#37;30
  &#37;31 &#61; add i64 &#37;30, 100
  &#37;scevgep11 &#61; getelementptr double, double addrspace&#40;13&#41;* &#37;29, i64 &#37;31
  &#37;32 &#61; mul i64 &#37;13, &#37;indvar
  &#37;33 &#61; mul i64 &#37;20, &#37;indvar
  &#37;34 &#61; add nsw i64 &#37;value_phi, -1
  &#37;35 &#61; mul i64 &#37;13, &#37;34
  &#37;36 &#61; mul i64 &#37;20, &#37;34
  &#37;37 &#61; mul i64 &#37;27, &#37;34
  &#37;38 &#61; add i64 &#37;33, 100
  &#37;scevgep19 &#61; getelementptr double, double addrspace&#40;13&#41;* &#37;22, i64 &#37;38
  &#37;scevgep17 &#61; getelementptr double, double addrspace&#40;13&#41;* &#37;22, i64 &#37;33
  &#37;39 &#61; add i64 &#37;32, 100
  &#37;scevgep15 &#61; getelementptr double, double addrspace&#40;13&#41;* &#37;15, i64 &#37;39
  &#37;scevgep13 &#61; getelementptr double, double addrspace&#40;13&#41;* &#37;15, i64 &#37;32
  &#37;bound0 &#61; icmp ult double addrspace&#40;13&#41;* &#37;scevgep, &#37;scevgep15
  &#37;bound1 &#61; icmp ult double addrspace&#40;13&#41;* &#37;scevgep13, &#37;scevgep11
  &#37;found.conflict &#61; and i1 &#37;bound0, &#37;bound1
  &#37;bound021 &#61; icmp ult double addrspace&#40;13&#41;* &#37;scevgep, &#37;scevgep19
  &#37;bound122 &#61; icmp ult double addrspace&#40;13&#41;* &#37;scevgep17, &#37;scevgep11
  &#37;found.conflict23 &#61; and i1 &#37;bound021, &#37;bound122
  &#37;conflict.rdx &#61; or i1 &#37;found.conflict, &#37;found.conflict23
  br i1 &#37;conflict.rdx, label &#37;scalar.ph, label &#37;vector.ph

vector.ph:                                        ; preds &#61; &#37;L2
  br label &#37;vector.body

vector.body:                                      ; preds &#61; &#37;vector.body, &#37;
vector.ph
  &#37;index &#61; phi i64 &#91; 0, &#37;vector.ph &#93;, &#91; &#37;index.next, &#37;vector.body &#93;
  &#37;offset.idx &#61; or i64 &#37;index, 1
;  @ none:3 within &#96;inner_noalloc_ib&#33;&#39;
; ┌ @ array.jl:730 within &#96;getindex&#39;
   &#37;40 &#61; add nsw i64 &#37;offset.idx, -1
   &#37;41 &#61; add i64 &#37;40, &#37;35
   &#37;42 &#61; getelementptr inbounds double, double addrspace&#40;13&#41;* &#37;15, i64 &#37;41
   &#37;43 &#61; bitcast double addrspace&#40;13&#41;* &#37;42 to &lt;4 x double&gt; addrspace&#40;13&#41;*
   &#37;wide.load &#61; load &lt;4 x double&gt;, &lt;4 x double&gt; addrspace&#40;13&#41;* &#37;43, align 8
   &#37;44 &#61; add i64 &#37;40, &#37;36
   &#37;45 &#61; getelementptr inbounds double, double addrspace&#40;13&#41;* &#37;22, i64 &#37;44
   &#37;46 &#61; bitcast double addrspace&#40;13&#41;* &#37;45 to &lt;4 x double&gt; addrspace&#40;13&#41;*
   &#37;wide.load24 &#61; load &lt;4 x double&gt;, &lt;4 x double&gt; addrspace&#40;13&#41;* &#37;46, align
 8
; └
; ┌ @ float.jl:395 within &#96;&#43;&#39;
   &#37;47 &#61; fadd &lt;4 x double&gt; &#37;wide.load, &#37;wide.load24
; └
;  @ none:4 within &#96;inner_noalloc_ib&#33;&#39;
; ┌ @ array.jl:769 within &#96;setindex&#33;&#39;
   &#37;48 &#61; add i64 &#37;40, &#37;37
   &#37;49 &#61; getelementptr inbounds double, double addrspace&#40;13&#41;* &#37;29, i64 &#37;48
   &#37;50 &#61; bitcast double addrspace&#40;13&#41;* &#37;49 to &lt;4 x double&gt; addrspace&#40;13&#41;*
   store &lt;4 x double&gt; &#37;47, &lt;4 x double&gt; addrspace&#40;13&#41;* &#37;50, align 8
   &#37;index.next &#61; add i64 &#37;index, 4
   &#37;51 &#61; icmp eq i64 &#37;index.next, 100
   br i1 &#37;51, label &#37;L25, label &#37;vector.body

scalar.ph:                                        ; preds &#61; &#37;L2
; └
;  @ none:2 within &#96;inner_noalloc_ib&#33;&#39;
  br label &#37;L5

L5:                                               ; preds &#61; &#37;L5, &#37;scalar.ph
  &#37;value_phi2 &#61; phi i64 &#91; 1, &#37;scalar.ph &#93;, &#91; &#37;63, &#37;L5 &#93;
;  @ none:3 within &#96;inner_noalloc_ib&#33;&#39;
; ┌ @ array.jl:730 within &#96;getindex&#39;
   &#37;52 &#61; add nsw i64 &#37;value_phi2, -1
   &#37;53 &#61; add i64 &#37;52, &#37;35
   &#37;54 &#61; getelementptr inbounds double, double addrspace&#40;13&#41;* &#37;15, i64 &#37;53
   &#37;55 &#61; load double, double addrspace&#40;13&#41;* &#37;54, align 8
   &#37;56 &#61; add i64 &#37;52, &#37;36
   &#37;57 &#61; getelementptr inbounds double, double addrspace&#40;13&#41;* &#37;22, i64 &#37;56
   &#37;58 &#61; load double, double addrspace&#40;13&#41;* &#37;57, align 8
; └
; ┌ @ float.jl:395 within &#96;&#43;&#39;
   &#37;59 &#61; fadd double &#37;55, &#37;58
; └
;  @ none:4 within &#96;inner_noalloc_ib&#33;&#39;
; ┌ @ array.jl:769 within &#96;setindex&#33;&#39;
   &#37;60 &#61; add i64 &#37;52, &#37;37
   &#37;61 &#61; getelementptr inbounds double, double addrspace&#40;13&#41;* &#37;29, i64 &#37;60
   store double &#37;59, double addrspace&#40;13&#41;* &#37;61, align 8
; └
; ┌ @ range.jl:594 within &#96;iterate&#39;
; │┌ @ promotion.jl:403 within &#96;&#61;&#61;&#39;
    &#37;62 &#61; icmp eq i64 &#37;value_phi2, 100
; │└
; │ @ range.jl:595 within &#96;iterate&#39;
; │┌ @ int.jl:53 within &#96;&#43;&#39;
    &#37;63 &#61; add nuw nsw i64 &#37;value_phi2, 1
; └└
  br i1 &#37;62, label &#37;L25, label &#37;L5

L25:                                              ; preds &#61; &#37;vector.body, &#37;
L5
; ┌ @ range.jl:594 within &#96;iterate&#39;
; │┌ @ promotion.jl:403 within &#96;&#61;&#61;&#39;
    &#37;64 &#61; icmp eq i64 &#37;value_phi, 100
; │└
; │ @ range.jl:595 within &#96;iterate&#39;
; │┌ @ int.jl:53 within &#96;&#43;&#39;
    &#37;65 &#61; add nuw nsw i64 &#37;value_phi, 1
; └└
  &#37;indvar.next &#61; add i64 &#37;indvar, 1
  br i1 &#37;64, label &#37;L36, label &#37;L2

L36:                                              ; preds &#61; &#37;L25
  ret &#37;jl_value_t addrspace&#40;10&#41;* addrspacecast &#40;&#37;jl_value_t* inttoptr &#40;i64 
87752712 to &#37;jl_value_t*&#41; to &#37;jl_value_t addrspace&#40;10&#41;*&#41;
&#125;
</pre>


<p>If you look closely, you will see things like:</p>
<pre><code>&#37;wide.load24 &#61; load &lt;4 x double&gt;, &lt;4 x double&gt; addrspac&#40;13&#41;* &#37;46, align 8
; └
; ┌ @ float.jl:395 within &#96;&#43;&#39;
&#37;47 &#61; fadd &lt;4 x double&gt; &#37;wide.load, &#37;wide.load24</code></pre>
<p>What this is saying is that it&#39;s loading and adding 4 <code>Float64</code>s at a time&#33; This feature of the processor is known as SIMD: single input multiple data. If certain primitive floating point operations, like <code>&#43;</code> and <code>*</code>, are done in succession &#40;i.e. no inbounds checks between them&#33;&#41;, then the processor can lump them together and do multiples at once. Since clock cycles have stopped improving while transistors have gotten smaller, this &quot;lumping&quot; has been a big source of speedups in computational mathematics even though the actual <code>&#43;</code> and <code>*</code> hasn&#39;t gotten faster. Thus to get full speed we want to make sure this is utilized whenever possible, which essentially just amounts to doing type inferred loops with no branches or bounds checks in the way.</p>
<h3>FMA</h3>
<p>Modern processors have a single operation that fuses the multiplication and the addition in the operation <code>x*y&#43;z</code>, known as a <em>fused multiply-add</em> or FMA. Note that FMA has less floating point roundoff error than the two operation form. We can see this intrinsic in the resulting LLVM IR:</p>


<pre class='hljl'>
<span class='hljl-nd'>@code_llvm</span><span class='hljl-t'> </span><span class='hljl-nf'>fma</span><span class='hljl-p'>(</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>5.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>3.0</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
;  @ floatfuncs.jl:319 within &#96;fma&#39;
; Function Attrs: uwtable
define double @julia_fma_15823&#40;double, double, double&#41; #0 &#123;
top:
; ┌ @ floatfuncs.jl:308 within &#96;fma_llvm&#39;
   &#37;3 &#61; call double @llvm.fma.f64&#40;double &#37;0, double &#37;1, double &#37;2&#41;
; └
  ret double &#37;3
&#125;
</pre>


<p>The Julia function <code>muladd</code> will automatically choose between FMA and the original form depending on the availability of the routine in the processor. The MuladdMacro.jl package has a macro <code>@muladd</code> which pulls apart statements to add <code>muladd</code> expressions. For example, <code>x1*y1 &#43; x2*y2 &#43; x3*y3</code> can be rewritten as:</p>
<pre><code>muladd&#40;x1,y1,muladd&#40;x2,y2,x3*y3&#41;&#41;</code></pre>
<p>Which reduces the linear combination to just 3 arithmetic operations. FMA operations can be SIMD&#39;d.</p>
<h3>Inlining</h3>
<p>All of this would go to waste if function call costs of 50 clock cycles were interrupting every single <code>&#43;</code>. Fortunately these function calls disappear during the compilation process due to what&#39;s known as inlining. Essentially, if the function call is determined to be &quot;cheap enough&quot;, the actual function call is removed and the code is basically pasted into the function caller. We can force a function call to occur by teling it to not inline:</p>


<pre class='hljl'>
<span class='hljl-nd'>@noinline</span><span class='hljl-t'> </span><span class='hljl-nf'>fnoinline</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>,</span><span class='hljl-n'>y</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>y</span><span class='hljl-t'>
</span><span class='hljl-nf'>finline</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>,</span><span class='hljl-n'>y</span><span class='hljl-p'>)</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>+</span><span class='hljl-t'> </span><span class='hljl-n'>y</span><span class='hljl-t'> </span><span class='hljl-cs'># Can add @inline, but this is automatic here</span><span class='hljl-t'>
</span><span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>qinline</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>,</span><span class='hljl-n'>y</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>4</span><span class='hljl-t'>
  </span><span class='hljl-n'>b</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-t'>
  </span><span class='hljl-n'>c</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>finline</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-n'>d</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>finline</span><span class='hljl-p'>(</span><span class='hljl-n'>b</span><span class='hljl-p'>,</span><span class='hljl-n'>c</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-nf'>finline</span><span class='hljl-p'>(</span><span class='hljl-n'>d</span><span class='hljl-p'>,</span><span class='hljl-n'>y</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span><span class='hljl-t'>
</span><span class='hljl-k'>function</span><span class='hljl-t'> </span><span class='hljl-nf'>qnoinline</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>,</span><span class='hljl-n'>y</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-n'>a</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>4</span><span class='hljl-t'>
  </span><span class='hljl-n'>b</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-ni'>2</span><span class='hljl-t'>
  </span><span class='hljl-n'>c</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>fnoinline</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>,</span><span class='hljl-n'>a</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-n'>d</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>fnoinline</span><span class='hljl-p'>(</span><span class='hljl-n'>b</span><span class='hljl-p'>,</span><span class='hljl-n'>c</span><span class='hljl-p'>)</span><span class='hljl-t'>
  </span><span class='hljl-nf'>fnoinline</span><span class='hljl-p'>(</span><span class='hljl-n'>d</span><span class='hljl-p'>,</span><span class='hljl-n'>y</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-k'>end</span>
</pre>


<pre class="output">
qnoinline &#40;generic function with 1 method&#41;
</pre>



<pre class='hljl'>
<span class='hljl-nd'>@code_llvm</span><span class='hljl-t'> </span><span class='hljl-nf'>qinline</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
;  @ none:2 within &#96;qinline&#39;
; Function Attrs: uwtable
define double @julia_qinline_15824&#40;double, double&#41; #0 &#123;
top:
;  @ none:4 within &#96;qinline&#39;
; ┌ @ none:1 within &#96;finline&#39;
; │┌ @ promotion.jl:313 within &#96;&#43;&#39; @ float.jl:395
    &#37;2 &#61; fadd double &#37;0, 4.000000e&#43;00
; └└
;  @ none:5 within &#96;qinline&#39;
; ┌ @ none:1 within &#96;finline&#39;
; │┌ @ promotion.jl:313 within &#96;&#43;&#39; @ float.jl:395
    &#37;3 &#61; fadd double &#37;2, 2.000000e&#43;00
; └└
;  @ none:6 within &#96;qinline&#39;
; ┌ @ none:1 within &#96;finline&#39;
; │┌ @ float.jl:395 within &#96;&#43;&#39;
    &#37;4 &#61; fadd double &#37;3, &#37;1
; └└
  ret double &#37;4
&#125;
</pre>



<pre class='hljl'>
<span class='hljl-nd'>@code_llvm</span><span class='hljl-t'> </span><span class='hljl-nf'>qnoinline</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
;  @ none:2 within &#96;qnoinline&#39;
; Function Attrs: uwtable
define double @julia_qnoinline_15825&#40;double, double&#41; #0 &#123;
top:
;  @ none:4 within &#96;qnoinline&#39;
  &#37;2 &#61; call double @julia_fnoinline_15826&#40;double &#37;0, i64 4&#41;
;  @ none:5 within &#96;qnoinline&#39;
  &#37;3 &#61; call double @julia_fnoinline_15827&#40;i64 2, double &#37;2&#41;
;  @ none:6 within &#96;qnoinline&#39;
  &#37;4 &#61; call double @julia_fnoinline_15828&#40;double &#37;3, double &#37;1&#41;
  ret double &#37;4
&#125;
</pre>


<p>We can see now that it keeps the function calls:</p>
<pre><code>&#37;4 &#61; call double @julia_fnoinline_21538&#40;double &#37;3, double &#37;1&#41;</code></pre>
<p>and this is slower in comparison to what we had before &#40;but it still infers&#41;.</p>


<pre class='hljl'>
<span class='hljl-n'>x</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nfB'>1.0</span><span class='hljl-t'>
</span><span class='hljl-n'>y</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nfB'>2.0</span><span class='hljl-t'>
</span><span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>qinline</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>,</span><span class='hljl-n'>y</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
16.015 ns &#40;1 allocation: 16 bytes&#41;
9.0
</pre>



<pre class='hljl'>
<span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>qnoinline</span><span class='hljl-p'>(</span><span class='hljl-n'>x</span><span class='hljl-p'>,</span><span class='hljl-n'>y</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
19.338 ns &#40;1 allocation: 16 bytes&#41;
9.0
</pre>


<p>Note that if we ever want to go the other direction and tell Julia to inline as much as possible, one can use the macro <code>@inline</code>.</p>
<h3>Summary</h3>
<ul>
<li><p>Scalar operations are super cheap, and if they are cache-aligned then more than one will occur in a clock cycle.</p>
</li>
<li><p>Inlining a function will remove the high function call overhead.</p>
</li>
<li><p>Branch prediction is pretty good these days, so keep them out of super tight inner loops but don&#39;t worry all too much about them.</p>
</li>
<li><p>Cache misses are quite expensive the futher out it goes.</p>
</li>
</ul>
<h2>Note on Benchmarking</h2>
<p>Julia&#39;s compiler is smart. This means that if you don&#39;t try hard enough, Julia&#39;s compiler might get rid of your issues. For example, it can delete branches and directly compute the result if all of the values are known at compile time. So be very careful when benchmarking: your tests may have just compiled away&#33;</p>
<p>Notice the following:</p>


<pre class='hljl'>
<span class='hljl-nd'>@btime</span><span class='hljl-t'> </span><span class='hljl-nf'>qinline</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>)</span>
</pre>


<pre class="output">
0.001 ns &#40;0 allocations: 0 bytes&#41;
9.0
</pre>


<p>Dang, that&#39;s much faster&#33; But if you look into it, Julia&#39;s compiler is actually &quot;cheating&quot; on this benchmark:</p>


<pre class='hljl'>
<span class='hljl-nf'>cheat</span><span class='hljl-p'>()</span><span class='hljl-t'> </span><span class='hljl-oB'>=</span><span class='hljl-t'> </span><span class='hljl-nf'>qinline</span><span class='hljl-p'>(</span><span class='hljl-nfB'>1.0</span><span class='hljl-p'>,</span><span class='hljl-nfB'>2.0</span><span class='hljl-p'>)</span><span class='hljl-t'>
</span><span class='hljl-nd'>@code_llvm</span><span class='hljl-t'> </span><span class='hljl-nf'>cheat</span><span class='hljl-p'>()</span>
</pre>


<pre class="output">
;  @ none:1 within &#96;cheat&#39;
; Function Attrs: uwtable
define double @julia_cheat_15878&#40;&#41; #0 &#123;
top:
  ret double 9.000000e&#43;00
&#125;
</pre>


<p>It realized that <code>1.0</code> and <code>2.0</code> are constants, so it did what&#39;s known as <em>constant propogation</em>, and then used those constants inside of the function. It realized that the solution is always <code>9</code>, so it compiled the function that... spits out <code>9</code>&#33; So it&#39;s fast because it&#39;s not computing anything. So be very careful about propogation of constants and literals. In general this is a very helpful feature, but when benchmarking this can cause some weird behavior. If a micro benchmark is taking less than a nanosecond, check and see if the compiler &quot;fixed&quot; your code&#33;</p>
<h2>Conclusion</h2>
<p>Optimize your serial code before you parallelize. There&#39;s a lot to think about.</p>
<h1>Discussion Questions</h1>
<p>Here&#39;s a few discussion questions to think about performance engineering in scientific tasks:</p>
<ol>
<li><p>What are the advantages of a <code>Vector&#123;Array&#125;</code> vs a <code>Matrix</code>? What are the disadvantage? &#40;What&#39;s different?&#41;</p>
</li>
<li><p>What is a good way to implement a data frame?</p>
</li>
<li><p>What are some good things that come out of generic functions for free? What are some things you should watch out for with generic functions?</p>
</li>
</ol>



          <HR/>
          <div class="footer"><p>
          Published from <a href="optimizing.jmd">optimizing.jmd</a> using
          <a href="http://github.com/mpastell/Weave.jl">Weave.jl</a>
           on 2019-09-09.
          <p></div>


        </div>
      </div>
    </div>
  </BODY>
</HTML>
